gpt4 book ai didi

android - 使用 fileprovider 加载文本文件时出现 ERR_UNKNOWN_URL_SCHEME

转载 作者:行者123 更新时间:2023-11-29 19:23:41 26 4
gpt4 key购买 nike

ma​​nifest.xml 中的提供者

<!-- File Provider -->
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.test.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>

资源文件

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>

文件提供者的使用

Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
intent.startActivity(intent);

引用:here

描述:我无法使用此文件提供程序打开图像、文本文件和 pdf。当我打开文件文本文件时,它会给我 ERR_UNKNOWN_URL_SCHEME。如果我打开图像或 pdf,它什么也没有显示

最佳答案

您需要将“FLAG_GRANT_READ_URI_PERMISSION”标志添加到您的 Intent 中:

Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "*/*");
intent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
intent.startActivity(intent);

https://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION

FLAG_GRANT_READ_URI_PERMISSION If set, the recipient of this Intent will be granted permission to perform read operations on the URI in the Intent's data and any URIs specified in its ClipData. When applying to an Intent's ClipData, all URIs as well as recursive traversals through data or other ClipData in Intent items will be granted; only the grant flags of the top-level Intent are used.

关于android - 使用 fileprovider 加载文本文件时出现 ERR_UNKNOWN_URL_SCHEME,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41800793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com