安卓 7.0 : install . apk

标签 android installation

我的代码:

 public static void installApk(Context context, File apkFile) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uri = FileProvider.getUriForFile(context, 
        context.getApplicationContext().getPackageName() + ".provider", apkFile);

            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        } else {
            uri = Uri.fromFile(apkFile);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        context.startActivity(intent);

    }

apk文件路径:

/storage/emulated/0/Download/com.me.myapp-1.0.1.apk

获取 Uri :

content://com.me.myapp.provider/publicDir/Download/com.me.myapp-1.0.1.apk

我没有收到消息的错误。

最佳答案

我的小疏忽造成的错误。

1 list 中添加代码

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

2 res/xml/provider_paths.xml

  <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path
            name="publicDir" path="/"/>
    </paths>

3 安装

public static void installApk(Context context, File apkFile) {

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri uri = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", apkFile);
        intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    } else {
        uri = Uri.fromFile(apkFile);
    }
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(uri, "application/vnd.android.package-archive");
    context.startActivity(intent);
}

你需要这个网址

https://developer.android.com/reference/android/os/FileUriExposedException.html

https://developer.android.com/reference/android/support/v4/content/FileProvider.html

代码解决了我的问题。希望对你有帮助

关于安卓 7.0 : install . apk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41354348/

相关文章:

c++ - 从 USB 设备运行软件

python - Pandas 升级安装失败

java - 确定谷歌街景功能存在

android - kotlin webview android.view.View android.view.View.findViewById(int)' on a null object reference ko

android - 在 Kotlin 中初始化 SharedPreference 的正确方法

自定义安装处理程序的 Eclipse p2 替代方案

sql-server - 安装 SQL Server 2012 和 .Net Framework 4.5

mysql - 我尝试安装 MySQL Community,但它总是安装 MySQL Commercial

java - 从 NumberPicker 中选择一个值

android - inline fun 的计算参数内不允许可组合调用