Thanos Infosec
Thanos Infosec

Reputation: 57

Open camera and Capture image issue

I am trying to make a simple application which uses the system camera application to take picture. I have tested my app in android version 6 and my app opens device's camera successfully. But in some android v6 devices (like Samsung Galaxy A3) I get the following security exception:

java.lang.SecurityException: 
     at android.os.Parcel.readException (Parcel.java:1621)
     at android.os.Parcel.readException (Parcel.java:1574)
     at android.app.ActivityManagerProxy.startActivity (ActivityManagerNative.java:3182)
     at android.app.Instrumentation.execStartActivity (Instrumentation.java:1541)
     at android.app.Activity.startActivityForResult (Activity.java:4298)
     at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult (BaseFragmentActivityJB.java:50)
     at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:79)
     at android.app.Activity.startActivityForResult (Activity.java:4245)
     at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:859)
     at iforest.photogps.MainActivity.captureImage (MainActivity.java:203)
     at iforest.photogps.MainActivity.access$000 (MainActivity.java:73)
     at iforest.photogps.MainActivity$1.onClick (MainActivity.java:130)
     at android.view.View.performClick (View.java:5721)
     at android.widget.TextView.performClick (TextView.java:10930)
     at android.view.View$PerformClick.run (View.java:22620)
     at android.os.Handler.handleCallback (Handler.java:739)
     at android.os.Handler.dispatchMessage (Handler.java:95)
     at android.os.Looper.loop (Looper.java:148)
     at android.app.ActivityThread.main (ActivityThread.java:7331)
     at java.lang.reflect.Method.invoke (Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
     at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

I already add permission checks to my code:

if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED 
         && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED 
         && ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    //TO do here if permission is granted by user
} else {
    //ask for permission if user didnot given
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}

and later on I call capture image:

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File photoFile = null;
    try {
        photoFile = createImageFile();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    //fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    String authorities = getApplicationContext().getPackageName() + ".provider";
    Uri imageUri = FileProvider.getUriForFile(this, authorities, photoFile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);  <-- line 203

}

From the logs I see captureImage line 203 with startActivityForResult is called. Since I already ask for permissions of camera why I get SecurityException to open camera in specific device Samsung Galaxy A3 -Android 6? In other devices with same version no error occurs to open camera.

Upvotes: 1

Views: 210

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007624

I am trying to make a simple application which uses the system camera application to take picture

There is no single "system camera application". Your code will start whatever camera app the user wants to use, and there will be hundreds of such apps spread across the ~10,000 Android device models and ~2 billion Android devices.

Since I already ask for permissions of camera why I get SecurityException to open camera in specific device Samsung Galaxy A3 -Android 6?

Replace:

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

with:

intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

and see if that helps.

Upvotes: 0

Related Questions