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

标签 android android-fileprovider

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/

相关文章:

android - 在服务中使用游标或通过绑定(bind)到服务发送数据

android - 如何隐藏Android操作栏中的菜单项?

android - 从API 24以上版本的应用程序打开APK文件

android - 如何处理无法解码流 : java. io.FileNotFoundException:使用 FileProvider(没有这样的文件或目录)

android - 使用 FileProvider 附件按 Intent 添加到电子邮件的杂散文件路径

android - import android.os.storage.StorageVolume 无法解析

android - Fragment 将所有导航控制委托(delegate)给 Activity 是个好主意吗?

android - 如何在 xml 选择器中设置 alpha

android - 为手机上的标准下载目录创建文件提供程序,以通过 Intent 发送 PDF 文件

具有特定文件扩展名的 Android 文件选择器