简体   繁体   English

打算安装APK文件-Android牛轧糖

[英]Intent to install APK file - Android Nougat

I searched extensively and did not find the problem. 我进行了广泛搜索,但没有发现问题。 When you try to install an APK file using an Intent in Android Nougat , it simply does not install and displays the following warning: "There was a problem parsing the package". 当您尝试在Android Nougat使用Intent安装APK file ,它根本不会安装,并显示以下警告:“解析软件包时出现问题”。

It works perfectly to open PDF files, for example, with settings to open this type of file (. PDF ). 例如,使用打开这种类型的文件( PDF )的设置,它可以完美地打开PDF文件。 However to install . 但是要安装。 apk files does not work. apk文件不起作用。

LogCat does not show any errors and I can not reach any solution. LogCat没有显示任何错误,我LogCat任何问题。

What could be wrong? 有什么事吗

The following code: 如下代码:

Manifest : 清单

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

Please, what could be wrong with this code? 拜托,这段代码可能出什么问题了? Can someone help me? 有人能帮我吗?

I had to change the intent for N (and higher) and remove the type designation. 我必须更改N(或更高)的意图,并删除类型名称。 Once I did that the install worked as expected. 一旦完成,安装将按预期进行。

So for 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM