从应用程序共享它时更改apk文件名

时间:2017-07-01 13:30:26

标签: java android apk

我使用以下代码分享我的应用程序:

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",如何更改?

1 个答案:

答案 0 :(得分:1)

为了与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;
}

现在使用您复制的文件创建一个意图。

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