安装APK文件的意图 - Android Nougat

8

我进行了广泛的搜索,但没有找到问题所在。当您尝试在Android Nougat中使用Intent安装一个APK文件时,它根本不会安装,并显示以下警告:“解析软件包时出现问题”。

例如,使用设置打开此类文件(.PDF)可以完美地打开PDF文件。然而,安装.apk文件却无法工作。

LogCat没有显示任何错误,我找不到任何解决方案。

可能有什么问题?

以下是代码:

清单文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="br.com.xxxxxx.xxxxxx.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepaths"/>
    </provider>

XML / 文件路径:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="storage/emulated/0" path="."/>

Activity:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        try {
            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            File file = new File(getContext().getFilesDir(), "app-debug.apk");
            Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);

            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }else{
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(Environment.getExternalStorageDirectory() + "/app-debug.apk");

            intent.setAction(android.content.Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            startActivity(intent);
            finish();
        } catch (Exception e) {
            e.getMessage();
        }
    }

请问这段代码有什么问题吗?有人可以帮我吗?

搜索了很多次,但没有找到问题。然后再试一下,因为这个问题几周前在这里已经报告过了。 - greenapps
你找到任何解决方案了吗? - Gautam
我在这个路径上遇到了错误。你能否再检查一下以找到解决方案? - NovusMobile
1个回答

3

我不得不更改N(及更高版本)的意图并删除类型标识。一旦我这样做了,安装就按预期进行了。

所以对于N:

Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(getContext().getFilesDir(), "app-debug.apk");
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file);
intent.setData(uri)
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
finish();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接