【问题标题】:Intent to install APK file - Android Nougat打算安装 APK 文件 - Android Nougat
【发布时间】:2017-05-18 14:06:25
【问题描述】:

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

它可以完美地打开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="."/>

活动:

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();
        }
    }

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

【问题讨论】:

  • searched extensively and did not find the problem. 那就试试更好,因为这个问题几周前才在这里报告过。
  • 你找到解决办法了吗?
  • 我正面临路径错误。你能再检查一下这个解决方案吗?

标签: android android-intent apk android-install-apk android-7.0-nougat


【解决方案1】:

我不得不更改 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();

【讨论】:

    猜你喜欢
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2019-06-06
    • 1970-01-01
    • 2019-03-10
    • 2012-12-01
    • 2013-08-11
    相关资源
    最近更新 更多