首页 > 解决方案 > 无法在第三方应用程序中打开 Zip 文件

问题描述

我正在尝试加载下载到context.getFileDir()第三方应用程序中的 zip 文件

第三方的结果是“*No files are selected *” 我试图检查 zip 文件是否下载成功,所以我已经从代码中解压缩它,它似乎下载正确,应该在外部第三方中打开党的申请。

我已将 Provider 放入清单以导出 URI

<provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                xmlns:tools="http://schemas.android.com/tools"
                tools:replace="android:resource"
                android:resource="@xml/provider_paths"/>
        </provider>

而我的道路

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

这就是我想要做的事情

File file = FileHelper.GET_FILE(material);
                try {
                    unzip(file);
                } catch (IOException e) {
                    e.printStackTrace();
                }

Uri uri = FileProvider.getUriForFile(material.getContext(), material.getContext(). 
getApplicationContext().getPackageName() +
 ".provider", file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/zip");
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        material.getContext().startActivity(intent);

标签: androidzipandroid-fileprovider

解决方案


似乎我忘记将此标志添加到意图中

intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

添加此标志后,一切正常


推荐阅读