我使用此代码将应用的APK文件发送到另一台设备。它适用于android 2.3.3,但不适用于android 4 +。
问题出在哪里?
我已经记录了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 :(得分: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)
更改行:
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)));
上述两种方式都适合我。
答案 2 :(得分:0)
您可以使用getApplicationInfo().publicSourceDir
代替getApplication().getPackageCodePath()
来获取应用APK的路径,然后在ACTION_SEND
Intent
中使用该路径通过蓝牙发送文件。< / p>
点击此处查看示例: Bluetooth File transfer on Android(even restricted types)
答案 3 :(得分: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))));
}
答案 4 :(得分: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"));
答案 5 :(得分: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));
使用 / 这对我有用