简体   繁体   English

Android 11 上的 FileProvider 共享文件

[英]FileProvider sharing file on Android 11

I'm facing a weird issue.我面临一个奇怪的问题。 I'm running on Samsung Galaxy S20FE Android 11. I want to open a PDF file from my app so as I've searched around I need to use a fileProvider.我在三星 Galaxy S20FE Android 11 上运行。我想从我的应用程序中打开一个 PDF 文件,所以我四处搜索我需要使用 fileProvider。 This is how I declared it in the manifest:这就是我在清单中声明它的方式:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

This is the XML file:这是 XML 文件:

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

The file is located inside the app's directory but on SD and not in internal storage of the device.该文件位于应用程序的目录内,但在 SD 上,而不是在设备的内部存储中。 I'm calling the function to open PDF file:我正在调用 function 打开 PDF 文件:

File file = new File(pdfpath);
Uri path = FileProvider.getUriForFile(this, "package.myapp.provider", file);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(path, "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION);

        try {
            startActivity(intent);
        }
        catch (ActivityNotFoundException e) {
            Toast.makeText(MainActivity.this,
                    "No Application Available to View PDF",
                    Toast.LENGTH_SHORT).show();
        }
    }

According to anywhere I've searched I don't see what I'm missing.根据我搜索过的任何地方,我看不到我缺少什么。 I also tried something for older version that should give UriPermissions to all needed packages:我还为旧版本尝试了一些应该为所有需要的包提供 UriPermissions 的东西:

List<ResolveInfo> resInfoList = context.getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);


for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    context.grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | 
         Intent.FLAG_GRANT_READ_URI_PERMISSION);
}

But no matter what I try, I always end up with the same exception:但无论我尝试什么,我总是以同样的例外结束:

2021-05-25 18:04:03.556 8873-8907/package.myapp E/DatabaseUtils: Writing exception to parcel
java.lang.SecurityException: Permission Denial: writing android.support.v4.content.FileProvider uri content://package.myapp.provider/root/0A18-B25A/Android/Data/package.myapp/gis/pdf/dragunov.pdf from pid=13365, uid=10104 requires the provider be exported, or grantUriPermission()

I don't understand why I'm getting permission denied.我不明白为什么我的权限被拒绝。 Also, I've tried setting the XML property to export the FileProvider but I ended up with getting an exception about that the provider can not be exported due to security so I decided not to try and go in that direction.此外,我尝试设置 XML 属性以导出 FileProvider,但我最终得到了一个异常,即由于安全原因无法导出提供程序,所以我决定不尝试 go 朝那个方向。

Any help would be appreciated.任何帮助,将不胜感激。

<root-path name="root" path="/storage/" />

You cannot use root-path anymore on an Android 11 device.您不能再在 Android 11 设备上使用根路径。

Think it was not even possible since Android Q.认为自从 Android Q 以来甚至不可能。

So FileProvider cannot serve from removable media now adays.所以 FileProvider 现在不能从可移动媒体中提供服务。

Use your own ContentProvider to do so.使用您自己的 ContentProvider 来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 权限拒绝:与 FileProvider Android 11 手机共享文件时出现安全异常 - Permission Denial: Security Exception while sharing file with FileProvider Android 11 phone 与FileProvider共享Android照片 - Android photo sharing with FileProvider Android FileProvider删除文件 - Android FileProvider delete file 使用 FileProvider 共享缓存文件而不附加文件 - Sharing a cache file using FileProvider not attaching the file 与 FileProvider 共享文件时的权限拒绝 - Permission Denial while sharing file with FileProvider 与FileProvider共享文件时使用grantUriPermission的SecurityException - SecurityException with grantUriPermission when sharing a file with FileProvider Android 11 应用间共享文件 - Android 11 sharing file between apps Android FileProvider无法找到文件 - Android FileProvider cannot find file Android 11:AndroidProtocolHandler:WebView 不通过 FileProvider 显示带有本地图像的本地 html 文件 - Android 11: AndroidProtocolHandler: WebView does not show local html file with local images via FileProvider Android Studio:通过 FileProvider 共享文件错误 E/FilePathConverter resolveFilePath 无法从 CursorWindow 读取第 0 行,列 -1 - Android Studio: Sharing a file thru FileProvider error E/FilePathConverter resolveFilePath Couldn't read row 0, col -1 from CursorWindow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM