Set File Permissions when using FileProvider

My app has features to 'Attach File', 'Take Picture', 'Take Video' etc. I was passing a File Uri to a new Intent but am getting the FileUriExposedException in Nougat. Hence have modified the code to use FileProvider. I am getting the Content Uri fine, but I get java.io.FileNotFoundException when I try to read or upload the file/picture/video. Am I setting the permissions wrong? Or do I need to set permissions in another way?

Here is what I am doing:

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;
}

In shouldOverrideUrlLoading (where I am setting up the Intent):

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();
}

In 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();
        }
    }

In the Android Monitor my output shows I/System.out: PICKFILE_REQUEST /storage/32CF-12FE/DCIM/Camera/20170711_161710.jpg which I guess means the content Uri is correct

However, I am unable to upload the file java.io.FileNotFoundException: /storage/32CF-12FE/DCIM/Camera/20170711_161710.jpg: open failed: EACCES (Permission denied)

Does this mean my permissions are not set correctly? How do I correct this.


Your code looks fine, but instead of getting the file URI through FileProvider, use the file's absolute path. Use your mediaFile and get absolute path by mediaFile.getAbsolutePath() .

This will return a string which you can use to read the file.

链接地址: http://www.djcxy.com/p/68822.html

上一篇: Android没有这样的文件或目录例外

下一篇: 使用FileProvider时设置文件权限