2013-06-30 54 views
7

我使用此代碼將應用的APK文件發送到其他設備。它適用於android 2.3.3,但不適用於android 4+。發送APK文件android

問題在哪裏?

我已經登錄了getpackageCodePath(),它返回的是Android 4+上的APK文件,但是整個代碼不工作,並且當藍牙啓動時,它什麼也不發送。

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

你確定你的Android 4.0設備具備處理藍牙傳輸?你有沒有試過通過藍牙使用OI文件管理器發送文件? –

+0

是的,我可以通過我的電話與文件管理器發送文件 – Ata

+0

@Ata你有任何答案嗎? –

回答

1

更改行:

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

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

,它應該在所有的4.x的設備上運行。或者你也可以這樣做:

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

以上兩種方式都適用於我。

12

我使用下面的代碼發送APK。和它的作品

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我試過這個代碼,它在三星設備上工作,但沒有在Moto G2上工作。 請讓我知道如何解決這個問題 –

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

我希望它的工作:

// 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)); 

使用/這個工作對我來說