Android从sdcard以编程方式安装.apk错误-程序包解析错误

时间:2019-11-14 06:54:14

标签: android

我正在尝试使用以下代码在Download文件夹下安装存储在外部存储中的.apk文件。解析软件包时出现错误消息对话框。我正在使用从Google Play商店下载的生产.apk文件,也正在调试.apk文件。对于这两个文件,以编程方式安装.apk时,都会出现相同的错误屏幕。我可以手动安装相同的.apk文件,因此该文件中没有问题。我什至将此应用程序做成了 Device Owner (设备所有者)应用程序,但它仍然无法正常工作。有什么方法可以安装.apk文件?

            Uri mUri_ = Uri.parse(filePath);
            String packageName = "com.neo.apkinstaller";
            context.grantUriPermission(packageName, mUri_, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            try {
                Intent appInstallerIntent = null;
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.N) {
                    appInstallerIntent = new Intent(Intent.ACTION_VIEW);
                    appInstallerIntent.setDataAndType(Uri.fromFile(new File(inst_path)), "application/vnd.android.package-archive");
                } else {
                    Uri uri = FileProvider.getUriForFile(context, "com.neo.apkinstaller", new File(inst_path));
                    appInstallerIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                    appInstallerIntent.setData(uri);
                    appInstallerIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                }
                appInstallerIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(appInstallerIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
Manifest.xml 文件中的

文件提供者声明:

<provider
                android:name="android.support.v4.content.FileProvider"
                android:authorities="com.neo.apkinstaller"
                android:exported="false"
                android:multiprocess="true"
                android:process="@string/process"
                android:grantUriPermissions="true">
            <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths"/>
        </provider>

provider_paths.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<paths>

    <external-path
            name="external_files"
            path="."/>

    <files-path
            name="files"
            path="/"/>

    <cache-path
            name="cache"
            path="/"/>

    <external-files-path
            name="external_files"
            path="."/>


    <external-cache-path
            name="external_cache"
            path="."/>


    <files-path
            name="tmp"
            path="tmp/"/>

    <root-path
            name="sdcard1"
            path="." />
</paths>

错误屏幕截图 enter image description here TIA

1 个答案:

答案 0 :(得分:0)

您可以看一下本youtube教程的结尾。 https://www.youtube.com/watch?v=PmOQ0EUwXjA&list=PLemXnOew0Zz3PZY48lWHpWKMT5_D8844D&index=4&t=0s 以编程方式安装了apk。 您可以使用Dropbox忽略第一部分;)

源代码为:

private void AutoUpdate (String NameOfNewApplication, String downloadPathFrom)
{
    DropboxDownloadPathTo = NameOfNewApplication;
    DropboxDownloadPathFrom = downloadPathFrom;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(getApplicationContext(), "Download file ...", Toast.LENGTH_SHORT).show();
            Thread th = new Thread(new Runnable() {
                public void run() {
                    File file = new File(DropboxDownloadPathTo + DropboxDownloadPathFrom.substring(DropboxDownloadPathFrom.lastIndexOf('.')));
                    if (file.exists()) file.delete();
                    try {
                        FileOutputStream outputStream = new FileOutputStream(file);
                        main.dropboxAPI.getFile(DropboxDownloadPathFrom, null, outputStream, null);
                        getMain().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getApplicationContext(), "File successfully downloaded.", Toast.LENGTH_SHORT).show();
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    if(file.exists())
                    {
                        while (file.length() == 0)
                        {
                            try {Thread.sleep(100);} catch (InterruptedException e) {e.printStackTrace();}
                        }

                        Intent intent = new Intent(Intent.ACTION_VIEW);
                        intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }
            });
            th.start();
        }
    });
}