【问题标题】:Android install .apk from sdcard programmatically error - Package Parse errorAndroid 从 sdcard 以编程方式安装 .apk 错误 - 包解析错误
【发布时间】:2019-11-14 06:54:14
【问题描述】:

我正在尝试使用以下代码安装存储在“下载”文件夹下的外部存储中的 .apk 文件。我收到一个错误对话框,其中包含解析包时出现问题的消息。我正在使用从 google play store 下载的生产 .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>

错误截图 TIA

【问题讨论】:

    标签: android


    【解决方案1】:

    你可以看看这个 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();
            }
        });
    }
    

    【讨论】:

    • 即使使用相同的 Intent 调用,我也会在 Lenovo K5 note (Android M) 中收到解析包错误。在 android N 之上,我们需要一个文件提供程序。
    猜你喜欢
    • 2016-09-12
    • 2012-05-12
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    相关资源
    最近更新 更多