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

标签 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”,如何更改?

最佳答案

对于更改 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/

相关文章:

java - 使用 DSE 的 Java API 映射 Cassandra 物化 View

Java swing、带有 html 文本的 JLabel 在悬停时无法正确呈现

通过 HTML5 视频元素的 Java 多媒体流

java - 并行迁移巨大的 XML 文件列表

android - 如何以编程方式拍照

Java:如何在 CameraX previewView 上绘图?

java - onSwipe 事件未在 ViewPager 内触发

android - 亚行 - [INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION]

android - 如何通过 Bluestacks 在 Mac 上运行 apk 文件

android - 我忘记了我的 keystore 密码,我想在 Google Play 上安装我的 apk。我该怎么办?