我想在intent中添加.apk文件。我想创建一个“共享”按钮,它将通过蓝牙或任何其他能够发送应用程序的应用程序共享整个应用程序。如果可以通过其他方式完成,请告诉我! 感谢
答案 0 :(得分:2)
List(ApplicationInfo) mAppList=getPackageManager().getInstalledApplications(0);
ApplicationInfo item = mAppList.get(position);
public static void ShareAPK(ApplicationInfo item,Context ctx) {
try {
File srcFile = new File(item.publicSourceDir);
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setType("application/vnd.android.package-archive");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile));
ctx.startActivity(Intent.createChooser(share, "Sharing"));
} catch (Exception e) {
e.printtrace();
}
}
答案 1 :(得分:1)
使用ACTION_SEND操作共享二进制数据,并设置适当的MIME类型并将URI放在额外命名为EXTRA_STREAM的数据中。这常用于共享图像,但可用于共享任何类型的二进制内容
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("application/vnd.android.package-archive");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
详情;看到这个:Send binary data
答案 2 :(得分:0)
您可以添加应用APK的位置
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Uri uri = Uri.parse("/data/apps/"+getApplicationContext().getPackageName()+".apk");
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share via"));
请注意,如果您的应用或共享应用无法访问该文件,则可能会获得SecurityException
答案 3 :(得分:0)
public void shareAPKMine()
{
try {
PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.sourceDir;
File sourceLocation = new File(s);
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/shefi/"+"test.apk";
File targetLocation = new File(path);
Utils.copyDirectory(sourceLocation, targetLocation);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("application/vnd.android.package-archive");
shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Uri uri = Uri.parse(path);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(shareIntent, "Share"+" "+getResources().getString(R.string.app_name)));
}catch (Exception e) {
e.printStackTrace();
}
}
// If targetLocation does not exist, it will be created.
public static void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
// make sure the directory we plan to store the recording in exists
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create dir " + directory.getAbsolutePath());
}
InputStream in = new FileInputStream(sourceLocation);
OutputStream out = new FileOutputStream(targetLocation);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}