0

I'm new to android and I'd like to let users to share the app's apk.

Here is the function:

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();
    }
}

The emails are sent but without the .apk file. On the other hand If I remove i.shareIntent.setType("*/*"); I get this error:

Unable to find application to perform this action.

I know that this question has asked before, but I could not find my answer there.

What is wrong with the function and how can I fix it?

Community
  • 1
  • 1
narad
  • 499
  • 3
  • 6
  • 11
  • I would not assume that your path is valid. Use `setType()` with the proper MIME type. Do not provide `EXTRA_TEXT`, or else the other app may choose to honor it instead of the stream. – CommonsWare May 25 '16 at 19:11
  • Try to set the type correctly with e.g. `i.setType("application/vnd.android.package-archive");` – Eknoes May 25 '16 at 19:13
  • guys, using `i.setType("application/vnd.android.package-archive");` does not make any difference. – narad May 25 '16 at 19:19
  • @CommonsWare how to verify that the path is valid? – narad May 25 '16 at 19:23
  • I think that you can use `getApplicationInfo()` to derive the APK path. Be advised that neither you nor the other app might have access to this location. – CommonsWare May 25 '16 at 19:26
  • @CommonsWare will you please elaborate how to actually use `getApplicationInfo()` in this context? – narad May 25 '16 at 19:36
  • A Google search on `get apk path getapplicationinfo` turns up http://stackoverflow.com/a/34526950/115145 and many other questions/answers. I have not used any of them, and so I cannot vouch for them. – CommonsWare May 25 '16 at 19:39
  • @CoominWare, Indeed the link helped. Thanks! – narad May 25 '16 at 19:45

1 Answers1

-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"));
}