0

In my activity, I have the following code:

public void myMethod() {
    final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir" + File.separator);
    root.mkdirs();
    final String fname = "img_" + System.currentTimeMillis() + ".jpg";
    outputFileUri = Uri.fromFile(new File(root, fname));

    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri);
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 232);
    startActivityForResult(takePhotoIntent, 1);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 232) {
        myMethod();
    } else {
        System.out.println("returned...");
        ImageView imageView = (ImageView) findViewById(R.id.test_image_view);
        imageView.setImageURI(outputFileUri);
    }
}

My test device is a rooted nexus 6 on Android 6.0.1. When "myMethod" is called, it lets me take the photo, but upon returning to the activity I get the following error:

java.io.FileNotFoundException: /storage/emulated/0/MyDir/img_1466772411267.jpg: open failed: EACCES (Permission denied)

I have the valid permissions declared in my manifest:

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

<uses-feature android:name="android.hardware.camera"/>

This error happens on the end of the method (I guess because the prior takes some time to throw the error?) Anyway, from what I see, I appear to be doing things the way I should be. Does anything seem off?

CaptainForge
  • 1,365
  • 7
  • 21
  • 46

2 Answers2

0

Your code will work till Android 5.0 but not in marshmallow os.

As Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app even though you had given permission in manifest file.

So app need to ask permission from user at runtime and if user does not give that permission you will get permission denial error. So you have to manage code for that.

Refer following link for more details: https://developer.android.com/training/permissions/requesting.html

  • usually yes this could be the issue, but questioner pointed out in comment, that he already tried request at runtime.... – Opiatefuchs Jun 24 '16 at 13:30
0

Runtime Permissions for Android 6.0

private static final int REQUEST_RUNTIME_PERMISSION = 1;

     void checkPremission() {
            //select which permission you want
            final String permission = Manifest.permission.CAMERA;
            //final String permission = Manifest.permission.Storage;
                // if in fragment use getActivity()
            if (ContextCompat.checkSelfPermission(ActivityName.this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(ActivityName.this, permission)) {

                } else {
                    ActivityCompat.requestPermissions(ActivityName.this, new String[]{android.permission.CAMERA,android.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_RUNTIME_PERMISSION);
                }
            } else {
                // you have permission go ahead
                myMethod();
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
                case REQUEST_RUNTIME_PERMISSION:
                    final int numOfRequest = grantResults.length;
                    final boolean isGranted = numOfRequest == 1
                            && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
                    if (isGranted) {
                        // you have permission go ahead
                        myMethod();
                    }else{
                        // you dont have permission show toast
                    }
                    break;
                default:
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41