首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法使用FileProvider在Android中打开外部存储文件

无法使用FileProvider在Android中打开外部存储文件
EN

Stack Overflow用户
提问于 2017-02-20 03:25:54
回答 3查看 8.5K关注 0票数 3

我在使用Android打开外部存储文件时遇到了问题。

文件路径

file:///storage/emulated/0/Download/SamplePDFFile_5mb.pdf

初始实现在低于7的Android版本上完美地工作

代码语言:javascript
运行
复制
File file = new File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "SamplePDFFile_5mb.pdf");
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);

在下面的例外情况下,相同的实现在7上不起作用。

android.os.FileUriExposedException

因此,我发现了类似的问题,并跟踪,以修复新的安卓安全功能,以应用程序为目标目标24或更高。

新实现

代码语言:javascript
运行
复制
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "SamplePDFFile_5mb.pdf");
//old way
//Uri uri = Uri.fromFile(file);
//new way
Uri uri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".provider", file);

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);

provider_paths.xml

代码语言:javascript
运行
复制
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

AndroidManifest.xml

代码语言:javascript
运行
复制
<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/provider_paths"/>
</provider>

即使在Android、provider_paths.xml和FileProvider中添加了必需的标记之后,我也无法打开文件。打开活动将关闭itself.no错误或logcat中的消息。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-02-20 04:27:09

代码语言:javascript
运行
复制
public void getGROUPSTORAGEPremission(){
    int hasWriteContactsPermission = Splash_activity.this.checkSelfPermission(Manifest.permission_group.STORAGE);
    if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
        Splash_activity.this.requestPermissions(new String[]{  Manifest.permission_group.STORAGE},
                PERMISSION_GROUPSTORAGE);
    } else {
       File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "SamplePDFFile_5mb.pdf");
     //old way
     //Uri uri = Uri.fromFile(file);
     //new way
     Uri uri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".provider", file);

       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setDataAndType(uri, "application/pdf");
       intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       startActivity(intent);
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == PERMISSION_GROUPSTORAGE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission Granted
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "SamplePDFFile_5mb.pdf");
     //old way
     //Uri uri = Uri.fromFile(file);
     //new way
      Uri uri = FileProvider.getUriForFile(MainActivity.this, getPackageName() + ".provider", file);

       Intent intent = new Intent(Intent.ACTION_VIEW);
       intent.setDataAndType(uri, "application/pdf");
       intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
       startActivity(intent);
        } else {
            // Permission Denied
            //Toast.makeText(mContext, "PERMISSION_GROUPSTORAGE Denied", Toast.LENGTH_SHORT).show();
        }
    }
 }

在N需要检查运行时权限之后。

票数 4
EN

Stack Overflow用户

发布于 2017-11-18 21:25:00

也许晚了,但我通过向意图添加一个临时权限来解决这个问题,方法如下:

代码语言:javascript
运行
复制
    Uri path = FileProvider.getUriForFile(MainActivity.this, "com.example.root.somepackage.provider", pdfFile);
    Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
    pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // GRANT TEMPORARY READ PERMISSION
    pdfIntent.setDataAndType(path, "application/pdf");

    try{
        startActivity(pdfIntent);
    }

这很有帮助:授予内容提供程序URI权限

票数 1
EN

Stack Overflow用户

发布于 2019-07-23 22:42:47

这个简单的解决方案帮助我解决了这类问题。希望它能帮助你节省你的时间。

代码语言:javascript
运行
复制
public void open_file(String filename) {
    File path =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File file = new File(path, filename);

    // Get URI and MIME type of file
    Uri uri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".fileprovider", file);
    String mime = context.getContentResolver().getType(uri);

    // Open file with user selected app
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_VIEW);
    intent.setDataAndType(uri, mime);
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(intent);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42335973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档