使用FileProvider时设置文件权限
我的应用程序具有“附加文件”,“拍照”,“拍摄视频”等功能。我将File Uri传递给一个新的Intent,但获得了Nougat中的FileUriExposedException 。 因此修改了代码以使用FileProvider。 我得到的内容Uri很好,但是当我尝试读取或上传文件/图片/视频时,出现java.io.FileNotFoundException 。 我是否设置了权限错误? 或者我需要以另一种方式设置权限?
这是我正在做的事情:
Android_manifest.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>
provider_paths.xml:
 <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="files" path="."/>
    <external-path name="external_files" path="."/>
    </paths>
MainActivity.java:
/*
* Creating file uri to store image/video
*/
public Uri getOutputMediaFileUri() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), Config.IMAGE_DIRECTORY_NAME);               
if (!mediaStorageDir.exists()) {
    try {
        mediaStorageDir.mkdirs();
        Log.d(TAG, "Created directory " + mediaStorageDir.getPath());
    } catch (SecurityException e) {
        e.printStackTrace();
    }
}
if (mediaStorageDir != null) {
    mediaFile = new File(mediaStorageDir.getPath() + File.separator+ "IMG_1" + ".jpg");
    try {
        Uri photoURI = FileProvider.getUriForFile(MainActivity.this,BuildConfig.APPLICATION_ID + ".provider", mediaFile);
        return photoURI;
    } catch (IllegalArgumentException e) {
            Log.e("File Selector","The selected file can't be shared: " +mediaFile);         
    }
}
return null;
}
在shouldOverrideUrlLoading(我在设置意图)中:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("*/*");
                intent.addCategory(Intent.CATEGORY_OPENABLE);
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION );
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    intent.putExtra(Intent.EXTRA_MIME_TYPES,extraMimeTypes);
    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE,true);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
}
try {
    startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),PICKFILE_REQUEST_CODE);
} catch (android.content.ActivityNotFoundException ex) {
    Toast.makeText(getApplicationContext(), "Please install a File Explorer.",  Toast.LENGTH_SHORT).show();
}
在onActivityResult中:
case  PICKFILE_REQUEST_CODE:
    if (resultCode == RESULT_OK){
        Uri selectedFile = data.getData();
        try {
            filePath = getPath(this,selectedFile);
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        System.out.println("PICKFILE_REQUEST " + filePath);
        if(filePath != null){
            new UploadFileToServer().execute();
        }else{
            Toast.makeText(getApplicationContext(), " Please select from  File Explorer or Gallery ",  Toast.LENGTH_SHORT).show();
        }
    }
在Android监视器中,我的输出显示I / System.out:PICKFILE_REQUEST /storage/32CF-12FE/DCIM/Camera/20170711_161710.jpg ,我猜这意味着内容Uri是正确的
但是,我无法上传文件java.io.FileNotFoundException:/storage/32CF-12FE/DCIM/Camera/20170711_161710.jpg:打开失败:EACCES(Permission denied)
这是否意味着我的权限设置不正确? 我如何纠正这一点。
  你的代码看起来很好,但不是通过FileProvider获取文件URI,而是使用文件的绝对路径。  使用mediaFile并通过mediaFile.getAbsolutePath()获取绝对路径。 
这将返回一个可用于读取文件的字符串。
链接地址: http://www.djcxy.com/p/68821.html