无法通过意图分享Android应用程序的.apk

时间:2016-05-25 19:03:43

标签: android android-intent apk

我是Android新手,我想让用户分享应用的apk。

这是功能:

protected void shareApp(View view){
    Intent i = new Intent(Intent.ACTION_SEND);
    i.shareIntent.setType("*/*");
    Uri path = Uri.parse("/data/apps/"+getApplicationContext().getPackageName()+".apk");
    i.putExtra(Intent.EXTRA_SUBJECT, "Here is the fancy app");
    i.putExtra(Intent.EXTRA_TEXT, "Don't miss it!");
    i.putExtra(Intent.EXTRA_STREAM, path);
    try {
        startActivity(Intent.createChooser(i, "Share via"));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }
}

发送电子邮件但没有.apk文件。 另一方面,如果我删除i.shareIntent.setType("*/*");,我会收到此错误:

  

无法找到执行此操作的应用程序。

我知道这个问题已经问before,但我找不到答案。

该功能有什么问题,如何解决?

1 个答案:

答案 0 :(得分:-1)

protected void shareApp(View view){

    ApplicationInfo app = getApplicationContext().getApplicationInfo();
    String filePath = app.sourceDir;

    Intent intent = new Intent(Intent.ACTION_SEND);

    // MIME of .apk is "application/vnd.android.package-archive".
    // but Bluetooth does not accept this. Let's use "*/*" instead.
    intent.setType("*/*");


    // Append file and send Intent
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new 
    File(filePath)));`enter code here`
    startActivity(Intent.createChooser(intent, "Share app via"));
}