无法将图像写入Android SD卡(权限被拒绝)

时间:2016-03-16 15:56:11

标签: android image permission-denied android-contentresolver absolute-path

当我尝试写入文件时出现以下错误:

java.io.FileNotFoundException:/storage/extSdCard/DCIM/Camera/20160314_231954.jpg:open failed:EACCES(Permission denied)

我用来编写代码的是:

//  The correctURIString was obtained earlier, in my example here, it is:
//  /storage/extSdCard/DCIM/Camera/20160314_231954.jpg
file = new File(correctURIString);
out1 = new FileOutputStream(file);
//The b is a Bitmap that I was creating (rotating 90 Degrees)
b.compress(Bitmap.CompressFormat.JPEG, 90, out1);

首先,让我告诉你到目前为止我做了什么:

我的清单包括权限请求;

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

它们位于Application标签之外。

我也尝试了这些主题中的所有建议:

1)java.io.filenotfoundexception open failed eacces (permission denied) on device

2)Permission denied when writting into sdCard

3)Exception 'open failed: EACCES (Permission denied)' on Android

4)Getting rotation from ExifInterface always returns 0

5)Android Exception : java.io.IOException: open failed: EACCES (Permission denied)

这里的问题是,如果我使用我的文件浏览器程序(Astro文件管理器顺便说一句)来查找这张精确的照片,那么ACTUAL位置列为:

“3261-3265 / DCIM /相机/ 20160314_231954.jpg”

这与通过尝试获取绝对路径获得的内容无关。我用来获取绝对路径的代码是:

public String getAbsolutePath(Uri uri) {
    if(Build.VERSION.SDK_INT >= 19){
        String id = uri.getLastPathSegment();
        //Custom function here, just keeps the numbers (removes everything else)
        id = StringUtilities.keepNumbersOnly(id);
        final String[] imageColumns = {MediaStore.Images.Media.DATA };
        final String imageOrderBy = null;
        Uri tempUri = getUri();
        Cursor imageCursor = getActivity().getContentResolver().query(tempUri, imageColumns,
                MediaStore.Images.Media._ID + "=" + id, null, imageOrderBy);
        if (imageCursor.moveToFirst()) {
            return imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
        }else{
            return null;
        }
    }else{
        String[] projection = { MediaStore.MediaColumns.DATA };
        Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
}

此特定照片的此绝对路径将返回为:

/storage/extSdCard/DCIM/Camera/20160314_231954.jpg

如您所见,此绝对位置与实际位置不匹配,因此无法正常工作。我不确定如何正常工作。

有谁知道如何正确获取正确的绝对路径?

感谢所有人的帮助。

-Sil

编辑: 这被标记为重复,引用此链接:

java.io.filenotfoundexception open failed eacces (permission denied) on device

我已经在问题中讨论了这个问题,我已经在那里尝试了答案而没有成功。 (见问题)

截至2017-03-02,这尚未解决,但希望澄清它并非重复。

0 个答案:

没有答案