【问题标题】:Set File Permissions when using FileProvider使用 FileProvider 时设置文件权限
【发布时间】:2017-07-12 10:39:09
【问题描述】:

我的应用具有“附加文件”、“拍照”、“拍摄视频”等功能。我将 File Uri 传递给新的Intent,但我在 Nougat 中获取 FileUriExposedException。因此修改了代码以使用 FileProvider。我的 Content 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 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 Monitor 中,我的输出显示 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(权限被拒绝)

这是否意味着我的权限设置不正确?我该如何纠正这个问题。

【问题讨论】:

    标签: android uri android-fileprovider


    【解决方案1】:

    您的代码看起来不错,但不是通过 FileProvider 获取文件 URI,而是使用文件的绝对路径。使用您的mediaFile 并通过mediaFile.getAbsolutePath() 获取绝对路径。

    这将返回一个字符串,您可以使用它来读取文件。

    【讨论】:

    • 我以为现在推荐使用FileProvider?
    • File Provider 是推荐的,它可以帮助你不通过你已经做过的意图暴露你的URI!您拥有mediaFile 的绝对路径,因此只需从那里读取您的文件。
    猜你喜欢
    • 1970-01-01
    • 2016-02-06
    • 2014-04-28
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    • 2015-10-18
    相关资源
    最近更新 更多