2016-05-25 10 views
0

Ich bin neu bei Android und ich möchte die Benutzer die App apk teilen lassen. HierKann Android App nicht teilen .apk durch Absicht

ist die Funktion:

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

Die E-Mails gesendet werden, aber ohne die .apk Datei. Wenn Auf der anderen Seite entferne ich i.shareIntent.setType("*/*"); ich diesen Fehler:

Unable to find application to perform this action.

Ich weiß, dass diese Frage before gefragt, aber ich konnte meine Antwort dort nicht finden.

Was ist falsch an der Funktion und wie kann ich sie beheben?

+0

Ich würde nicht davon ausgehen, dass Ihr Pfad gültig ist. Verwenden Sie 'setType()' mit dem richtigen MIME-Typ. Geben Sie nicht 'EXTRA_TEXT' an, da die andere App diese Option anstelle des Streams übernehmen könnte. – CommonsWare

+0

Versuchen Sie den Typ korrekt einzustellen, z. 'i.setType (" application/vnd.android.package-archive ");' – Eknoes

+0

Leute, die 'i.setType (" application/vnd.android.package-archive ") verwenden;' macht keinen Unterschied. – narad

Antwort

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