1

I am getting the following error when I try to write to a File:

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

The code I am using to write it is this:

//  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);

First off, let me tell you what I have done thus far:

My manifest IS including the permissions requests;

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

And they are located just outside of the Application tag.

I've also tried all of the recommendations in these threads:

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)

The issue here is that if I use my file explorer program (Astro File Manager btw) to find this exact photo, the ACTUAL location is listed as:

"3261-3265/DCIM/Camera/20160314_231954.jpg"

Which is nothing matching what I am getting via the attempts to get Absolute Paths. The code I am using to get the absolute path is:

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

This absolute path for this particular photo is being returned as:

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

As you can see, this absolute location does not match the actual one, hence, it is not working. I am unsure how to make this work properly.

Does anyone have any idea how to correctly get the absolute path that is correct?

Thanks for the help all.

-Sil

Edit: This was marked as duplicate citing this link:

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

Which was already discussed in the question where I indicated I had tried the answers there without success. (See question)

As of 2017-03-02, this has not been solved, but wanting to clarify that it was not a duplicate.

Community
  • 1
  • 1
PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • What version of Android are you running this on? 6.0? – Pztar Mar 16 '16 at 15:59
  • Are your permissions requests in your manifest outside of the application tag? If not, as @Pztar is asking/hinting at, if you are running version 6.0 you need to look into explicitly requesting permission. – zgc7009 Mar 16 '16 at 16:01
  • I am running this on Jelly Bean, I have another section of the app handling Marshmallow specific requests. And with regards to the location of the permission requests in the manifest, as I mentioned in my post, "And they are located just outside of the Application tag" – PGMacDesign Mar 16 '16 at 16:16
  • 1
    I don't think you are supposed to separate them based on version. I think that you need to handle permissions for all api 23 compiled builds the same. May be mistaken, haven't dug into it too much. Also, sorry I missed that line earlier, just a common mistake with permissions. – zgc7009 Mar 16 '16 at 16:43
  • Quite true zgc, quite a common mistake. With regards to handling Marshmallow, I was shortening the story, but the short answer is, I got lazy and wrote a class that launches when the app opens. If the user denies the permissions I request, it will not allow them to proceed into the app, so by nature of that, they have to have allowed it by the time they get into the app proper. – PGMacDesign Mar 16 '16 at 16:57
  • Is it correct that you (1) read the file '/storage/extSdCard/DCIM/Camera/20160314_231954.jpg', (2) rotate the image and (3) when you write it back with the same path you get the error message? – k3b Mar 16 '16 at 17:07
  • Yes K3B, precisely correct. – PGMacDesign Mar 16 '16 at 17:14
  • @Silmarilos I am having same issue, Did you find solution to this issue – Ankesh kumar Jaisansaria Apr 05 '16 at 14:39
  • Sadly, I never found a solution. Best I could do was tell the user that they are unable to use that photo. – PGMacDesign Apr 05 '16 at 18:26

0 Answers0