Search code examples
androidpermissionsandroid-gallery

Rare Security Exception when loading an image from the gallery


I've been going through the crashes listed in Google Play Console for my app. The one crash I cannot seem to reproduce:

java.lang.RuntimeException: 
at android.app.ActivityThread.deliverResults (ActivityThread.java:3778)
at android.app.ActivityThread.handleSendResult (ActivityThread.java:3821)
at android.app.ActivityThread.access$1400 (ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1428)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:150)
at android.app.ActivityThread.main (ActivityThread.java:5659)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:822)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:712)
    Caused by: java.lang.SecurityException: 
at android.os.Parcel.readException (Parcel.java:1602)
at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel (DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query (ContentProviderNative.java:421)
at android.content.ContentResolver.query (ContentResolver.java:513)
at android.content.ContentResolver.query (ContentResolver.java:438)
at za.co.overtake.onlinetrucks.utils.Utils.getBitmapFromUri (Utils.java:381)
at za.co.overtake.onlinetrucks.fragments.DamagePanelDetailsDialogFragment.onActivityResult (DamagePanelDetailsDialogFragment.java:513)
at android.support.v4.app.FragmentActivity.onActivityResult (FragmentActivity.java:164)
at android.app.Activity.dispatchActivityResult (Activity.java:6602)
at android.app.ActivityThread.deliverResults (ActivityThread.java:3774)

The crash was reported to be on a Hauwei P8 Lite. I've tested on various devices (including a Hauwei P8) but cannot reproduce the issue! These are the permissions in my manifest file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

and here I request the user permissions:

public static void dispatchLoadGalleryEvent(Context context, Fragment frag) {
    if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(context,
            Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        PermissionUtil.requestWritePermission(context, frag, ACCESS_GALLERY_PERMISSION_CODE);
        return;
    }

    Intent i = new Intent(
            Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

    frag.startActivityForResult(i, ACCESS_GALLERY_REQUEST_CODE);
}

and then here I load the Image using the URI returned from the intent:

 public static Bitmap getBitmapFromUri(Context context, Uri selectedImage) {
    String[] filePathColumn = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn,
            null, null, null);
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    cursor.close();
    return (Utils.getBitmapFromFileWithCorrectOrientation(picturePath, 4));
}

The crash is reported to be on the context.getContentResolver().query() line. I feel like I'm doing something silly somewhere, and super confused that I cannot reproduce the issue.


Solution

  • SecurityException

    Thrown by the security manager to indicate a security violation.

    Throwable.getMessage() will give use specific information about the Security. Without message it is really difficult to identified the problem.

    Caused by: java.lang.SecurityException: <We got getMessage() data here but it is blank>

    That logcat came from Google PlayStore. It have so many possibilities about the user actions. Such as :-

    1. User Decline some of Permissions.

    2. User want to run your app without giving permission.

    3. I think you have to handle every permission validation. If user want to decline permission then you have to stop user view that page and functionality or just handle it so it will not crash if user decline any permission.

    and more.

    Just try to run your app while decline all permission and try to see it is running or not and find the suitable solution if app crashing anywhere.

    NOTE: -IT IS ALL ABOUT TESTING APP FROM BEGINNING TO END.