无法使用FileProvider在Android N中打开外部存储文件 [英] Can't open external storage file in Android N using FileProvider

查看:312
本文介绍了无法使用FileProvider在Android N中打开外部存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法使用Android N打开外部存储设备中的文件.

I am having trouble opening file inside external storage with Android N.

文件路径

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

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

初始实施 完美适用于低于7的Android版本

Initial implementation Perfectly work on Android version lower than 7

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.

The same implementation doesn't work on 7 with below exception.

android.os.FileUriExposedException

android.os.FileUriExposedException

所以我发现了类似的问题,并按照 this 以便将针对应用程序定位的新android安全功能修复为24或更高版本.

So i found out similar questions and follow this in order to fix new android security feature for app targeting to 24 or higher.

新实施

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

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

AndroidManifest.xml

<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 Manifest,provider_paths.xml和FileProvider中添加了必需的标签之后,我也无法打开该文件.打开活动本身关闭.logcat中没有错误或消息.

Even after adding required tag in Android Manifest, provider_paths.xml and FileProvider, i am not able to open the file. Opening activity close on itself.no error or messages in logcat.

推荐答案

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之后需要检查运行时权限.

After N need to check runtime permission .

这篇关于无法使用FileProvider在Android N中打开外部存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆