2013-06-30 14 views
7

Uso questo codice per inviare il file APK dell'app su un altro dispositivo. Funziona su Android 2.3.3, ma non funziona su Android 4+.Invio di file APK android

Dov'è il problema?

Ho registrato il getpackageCodePath() e restituisce il file APK su Android 4+, ma l'intero codice non funziona, e quando Bluetooth si avvia, non invia nulla.

ArrayList<Uri> uris = new ArrayList<Uri>(); 
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
sendIntent.setType("application/vnd.android.package-archive"); 
uris.add(Uri.parse(getApplication().getPackageCodePath())); 
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
startActivity(Intent.createChooser(sendIntent, null)); 
+0

Sei sicuro il vostro dispositivo Android 4.0 è in grado di gestire trasferimenti Bluetooth? Hai provato a inviare un file tramite Bluetooth usando OI File Manager o qualcosa del genere? –

+0

sì, e posso inviare file tramite il mio telefono con file manager – Ata

+0

@Ata Hai avuto qualche risposta? –

risposta

1

Modificare la riga:

uris.add(Uri.parse(getApplication().getPackageCodePath())); 

a

uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir))); 

e dovrebbe funzionare in tutti i dispositivi 4.x. In alternativa puoi fare qualcosa di simile a questo:

ApplicationInfo app=getPackageManager().getApplicationInfo("packageName", 0);      
uris.add(Uri.fromFile(new File(app.publicSourceDir))); 

Entrambi i modi sopra funzionano per me.

0

È possibile utilizzare getApplicationInfo().publicSourceDir invece di getApplication().getPackageCodePath() per ottenere il percorso per APK della tua app, quindi utilizzare che in un ACTION_SENDIntent per inviare il file tramite Bluetooth.

Controllare qui per un esempio: Bluetooth File transfer on Android(even restricted types)

12

Io uso qui di seguito il codice per l'invio apk. e funziona

try{ 
    ArrayList<Uri> uris = new ArrayList<Uri>(); 
    Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
    sendIntent.setType("application/*"); 
    uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir))); 
    sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
    startActivity(Intent.createChooser(sendIntent, null)); 

}catch(Exception e){} 
+1

+1 Ho provato questo codice, funziona su dispositivi Samsung ma non funziona su Moto G2. Per favore fatemi sapere come risolvere il problema –

0
InputStream in = null; 
      OutputStream out = null; 
      File apkPublicFilePath = new File(
        Environment 
          .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), 
        "myapk.apk"); 
      try { 

       in = getResources().openRawResource(R.raw.myapk); 
       out = new FileOutputStream(apkPublicFilePath); 
       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = in.read(buffer)) > 0) { 
        out.write(buffer, 0, length); 
       } 

       // Close the streams 
       out.flush(); 
       out.close(); 
       in.close(); 
      } catch (IOException e) { 
       Log.d(TAG, "Error in copy files"); 
      } 

      Intent sendIntent = new Intent(); 
      sendIntent.setAction(Intent.ACTION_SEND); 
      sendIntent.putExtra(Intent.EXTRA_STREAM, 

      Uri.fromFile(apkPublicFilePath.getAbsoluteFile())); 
      try { 
       sendIntent.setType("application/vnd.android.package-archive"); 
       startActivity(Intent.createChooser(
         sendIntent, 
         PersianReshape.reshape(getResources().getString(
           R.string.msg_send)))); 
      } catch (Exception e) { 
       sendIntent.setType("*/*"); 
       startActivity(Intent.createChooser(
         sendIntent, 
         PersianReshape.reshape(getResources().getString(
           R.string.msg_send)))); 
      } 
0

spero che funziona:

// Get current ApplicationInfo to find .apk path 
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("*/*"); 

// Only use Bluetooth to send .apk 
intent.setPackage("com.android.bluetooth"); 

// Append file and send Intent 
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))); 
startActivity(Intent.createChooser(intent, "Share app")); 
0
ArrayList<Uri> uris = new ArrayList<Uri>(); 
    Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); 
sendIntent.setType("*/*"); 
uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir))); 
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
startActivity(Intent.createChooser(sendIntent, null)); 

Usa / questo ha funzionato per me

Problemi correlati