将APK文件发送到android
我使用此代码将应用程序的APK文件发送到其他设备。它可以在安卓2.3.3上运行,但不能在安卓4+上运行 问题在哪里 我已经记录了将APK文件发送到android,android,bluetooth,apk,Android,Bluetooth,Apk,我使用此代码将应用程序的APK文件发送到其他设备。它可以在安卓2.3.3上运行,但不能在安卓4+上运行 问题在哪里 我已经记录了getpackageCodePath(),它在android 4+上返回APK文件,但是整个代码都不工作,当蓝牙启动时,它什么也不发送 arraylisturis=newarraylist(); Intent sendIntent=新的Intent(Intent.ACTION\u SEND\u多个); setType(“application/vnd.android.p
getpackageCodePath()
,它在android 4+上返回APK文件,但是整个代码都不工作,当蓝牙启动时,它什么也不发送
arraylisturis=newarraylist();
Intent sendIntent=新的Intent(Intent.ACTION\u SEND\u多个);
setType(“application/vnd.android.package归档”);
add(Uri.parse(getApplication().getPackageCodePath());
sendIntent.putParcelableArrayListExtra(Intent.EXTRA\u流,URI);
startActivity(Intent.createChooser(sendIntent,null));
更改行:
uris.add(Uri.parse(getApplication().getPackageCodePath()));
到
它应该在所有4.x设备中都能工作。或者,您也可以这样做:
ApplicationInfo app=getPackageManager().getApplicationInfo("packageName", 0);
uris.add(Uri.fromFile(new File(app.publicSourceDir)));
以上两种方法对我都适用。你可以使用
getApplicationInfo().publicSourceDir
而不是getApplication().getPackageCodePath()
来获取应用程序APK的路径,然后在操作中使用该路径发送意图通过蓝牙发送文件
请点击此处查看示例:
我使用下面的代码发送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){}
试试看{
ArrayList URI=新的ArrayList();
Intent sendIntent=新的Intent(Intent.ACTION\u SEND\u多个);
sendIntent.setType(“应用程序/*”);
add(Uri.fromFile(新文件(getApplicationInfo().publicSourceDir));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA\u流,URI);
startActivity(Intent.createChooser(sendIntent,null));
}捕获(例外e){}
我希望它能起作用:
// 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"));
arraylisturis=newarraylist();
Intent sendIntent=新的Intent(Intent.ACTION\u SEND\u多个);
sendIntent.setType(“*/*”);
add(Uri.fromFile(新文件(getApplicationInfo().publicSourceDir));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA\u流,URI);
startActivity(Intent.createChooser(sendIntent,null));
使用/这对我有用你确定你的Android 4.0设备配备了蓝牙传输功能吗?您是否尝试过使用OI文件管理器或其他工具通过蓝牙发送文件?是的,我可以通过手机发送带有文件的文件managers@Ata你们得到答案了吗?没有,看起来安卓4阻止了原始APK文件上的文件访问操作。它并没有被阻止,但它确实改变了。请参阅getApplicationInfo().publicSourceDir
+1我尝试了这段代码,它在三星设备上工作,但在Moto G2上不工作。请告诉我如何解决这个问题
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))));
}
// 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"));
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));