【问题标题】:Sharing Apk via Share option on NavigationDrawer通过 NavigationDrawer 上的共享选项共享 Apk
【发布时间】:2017-06-14 01:44:38
【问题描述】:

我正在尝试通过导航抽屉中的共享选项共享我的应用 apk。此代码适用于文本,但如何为 Apk 做呢?

else if(id==share){
Intent sharingIntent = new Intent("android.intent.action.send");
sharingIntent.setType("text/plain");
sharingIntent.putExtra("android.intent.extr.TEXT", "Theshared text");
startActivity(Intent.createChooser(sharingIntent, "Share Using"));
}

【问题讨论】:

  • 您是要共享 APK 本身还是要共享 playstore url?
  • APK 本身 @ArunShankar
  • 尝试首先搜索您的问题,人们已经怀疑您现在有。 See your solution here

标签: android android-fragments android-intent apk


【解决方案1】:

您可以使用以下代码分享您的 APK,

public static void shareAPK(Activity activity) {
    if (ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
        try {
            // First we should copy apk file from source dir to ur external dir
            ApplicationInfo app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), 0);

            File apkFile = new File(app.sourceDir);
            File backupFile = new File(Environment.getExternalStorageDirectory(), "[ANY NAME FOR APK].apk");

            copy(apkFile, backupFile);

            Intent shareIntent = getShareIntent(backupFile);
            activity.startActivity(Intent.createChooser(shareIntent, "Send [AppName] application APK using"));
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

public static Intent getShareIntent(File file) {
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
    intent.setType("application/vnd.android.package-archive");
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    return intent;
}


public static void copy(File src, File dst) throws IOException {
    FileInputStream inStream = new FileInputStream(src);
    FileOutputStream outStream = new FileOutputStream(dst);
    FileChannel inChannel = inStream.getChannel();
    FileChannel outChannel = outStream.getChannel();
    inChannel.transferTo(0, inChannel.size(), outChannel);
    inStream.close();
    outStream.close();
}

【讨论】:

    猜你喜欢
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 2013-08-07
    • 2022-11-24
    • 1970-01-01
    相关资源
    最近更新 更多