首页 > 解决方案 > 使用 Intent 构建、安装 APK

问题描述

当我尝试使用以下方式安装 APK 时:

val installIntent = Intent(Intent.ACTION_INSTALL_PACKAGE); 
    installIntent.setData(Uri.fromFile(true_path));
    startActivity(installIntent);

我收到错误:

file:///...app-debug.apk exposed beyond app through Intent.getData()

我相信意图不能使用 file:// URI;如何使用 Android Studio 构建 APK,以便 Intent 可以使用它?

标签: androidandroid-intentapk

解决方案


正确的做法是

File file = new File(dir, "App.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), 
"application/vnd.android.package-archive");
startActivity(intent);

推荐阅读