问题描述
我正在尝试将一个已下载到context.getFileDir()
的zip文件加载到第三方应用程序中
来自第三方的结果是“ *未选择文件*”我试图检查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);
解决方法
似乎我忘了将此标志添加到意图中
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
添加此标志后,一切正常运行