我用以下代码分享我的应用程序:
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath)));
startActivity(Intent.createChooser(intent, "Share app via"));
但是共享的文件名是“base.apk”,如何更改?
最佳答案
对于更改 apk 名称的共享应用程序,您可以使用以下方法将 apk 文件复制到您的 sd 卡,传递 ApplicationItem、预期文件夹和 apk 的新预期名称
public static File copyFile(ApplicationInfo appInfo, File folderName, String apkName) {
File initialFile = new File(appInfo.getSourceDir());
File finalFile = new File(folderName, apkName);
try {
FileInputStream inStream = new FileInputStream(initialFile);
FileOutputStream outStream = new FileOutputStream(finalFile);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return finalFile;
}
现在使用您复制的文件创建一个 Intent 。
public static Intent getShareIntent(File file) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
}
终于可以分享应用了,
ApplicationInfo app = getApplicationContext().getApplicationInfo();
File newFile = copyFile(app, Environment.getExternalStorageDirectory(), "[Expected name].apk");
Intent shareIntent = getShareIntent(newFile);
startActivity(Intent.createChooser(shareIntent, "Sharing [Appname] Apk"));
关于java - 从应用程序共享时更改 apk 文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44861505/