0

I'm having a rather strange problem with an Android app I'm writing.

In my app, I use an intent to tell the phone's camera app to take a picture for me. After I take this picture, I want to register the newly taken photo with the device's MediaStore content provider in order to make it show up in the regular "gallery" app.

To do this, I know I need the WRITE_EXTERNAL_STORAGE permission, but even when it's in my manifest I get a permission denied exception. What do I have to do to get this permission? I'm running Android 6.0.1.

Here's my permissions XML in the manifest:

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

Here's the code that causes the exception:

       // insert photo into phone's main photo content provider
        ContentResolver cr = getActivity().getContentResolver();

        try {
            MediaStore.Images.Media.insertImage(cr, photoFile.getPath(),
                    "Image Capture", "Custom Image capture");
        } catch (FileNotFoundException foe) {
            Log.e(DEBUG, foe.getMessage());
        }

And here's the exception I get:

java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2436, uid=10041 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

EDIT: I should probably mention:

Right now my app uses getExternalFilesDir(Environment.DIRECTORY_PICTURES), which returns the path /sdcard/Android/data/<package>/files/Pictures to save it's photos. I want them to go where the camera app normally puts them, in /sdcard/DCIM/Camera.

RGrun
  • 350
  • 3
  • 16

3 Answers3

0

You should add the android:maxSdkVersion="18" to the WRITE_EXTERNAL_STORAGE as well. Your code should look like this:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />
gatteo
  • 1,382
  • 2
  • 10
  • 17
0

This may be a problem with respect to you requesting the permission but not being granted the permission by the user. Have a look at my answer @ https://stackoverflow.com/a/35574084/529691

And see if that helps.

Community
  • 1
  • 1
JoxTraex
  • 13,423
  • 6
  • 32
  • 45
0

You should promt the user to provide you permission:

    int externalPermissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);
    if (externalPermissionCheck==-1){
        askPermissionStorage();
    }


private void askPermissionStorage() {
    //for media
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
            WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

        //insert explanation
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            //if you want to explaian to the user

        } else {//if no explanation needed
            ActivityCompat.requestPermissions(this, new
                            String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
        }
    }
}

The above is for save the picture\video and this is for the camera permission:

    int cameraPermissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA);
    if (cameraPermissionCheck == -1) {
        askPermissionCamera();
    }
private void askPermissionCamera() {
    //for camera
    if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.
            CAMERA) != PackageManager.PERMISSION_GRANTED) {
        //insert explanation
        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                Manifest.permission.CAMERA)) {
            //if you want to explaian to the user

        } else {//if no explanation needed
            ActivityCompat.requestPermissions(this, new
                            String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        }
    }
}
Yuval
  • 21
  • 1
  • 7