0
votes

I am trying to programmatically get the number of images (videos and pictures) in the gallery of my Android Tablet. It is an SM-T580 if that matters. I was able to get it to work with android phones, but can not get it to work with the tablet. It must throw an exception because the application bombs out, but it gives no meaningful information, even when I stacktrace. I am not sure what is going on here. I will include my code that works on phones, but bombs on the tablet. Any help is much appreciated.

Method to get all pictures in the gallery:

public int findAnyPictures(){
    int count = 0;
    final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
    final String orderBy = MediaStore.Images.Media._ID;
    //Stores all the images from the gallery in Cursor
    Cursor cursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
            null, orderBy);
    //Total number of images
    if (cursor != null){
        count = cursor.getCount();
    }
    cursor.close();
    return count;
}

Here is the method that counts all videos

 public int findAnyVideos() {
        int vidsCount = 0;
        Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String[] projection = { MediaStore.Video.VideoColumns.DATA };
        Cursor cursor = mContext.getContentResolver().query(uri, projection, null, null, null);
        if (cursor != null) {
            vidsCount = cursor.getCount();
            cursor.close();
        }
        return vidsCount;
    }

It seems to throw a runtime exception when find any pictures is called, I am not sure why it is telling me I do not have the proper permissions. I have included the permission it mentions in the android manifest and I have no idea what grantUriPermission() is? Here is the stack trace

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.thopfer.resetverify/com.example.thopfer.resetverify.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=22228, uid=10126 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3305) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3401) at android.app.ActivityThread.access$1100(ActivityThread.java:229) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:7303) 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) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=22228, uid=10126 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1620) 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:502) at android.content.ContentResolver.query(ContentResolver.java:445) at com.example.thopfer.resetverify.MainActivity.findAnyPictures(MainActivity.java:234) at com.example.thopfer.resetverify.MainActivity.onCreate(MainActivity.java:185) at android.app.Activity.performCreate(Activity.java:6904) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3252) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3401)  at android.app.ActivityThread.access$1100(ActivityThread.java:229)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:7303)  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)

 

1
Did you checked Runtime permissions in tablet? - Rahul
"It must throw an exception because the application bombs out, but it gives no meaningful information, even when I stacktrace" -- please edit your question and provide the full Java stack trace associated with your crash. We cannot help you understand that stack trace unless we can see it. - CommonsWare
Do not close() the cursor when null. - greenapps
I have added the stacktrace. It was not giving one before. I am not sure It says I need the permission included, but it is already in the andorid manifest. - Toby

1 Answers

1
votes

Well this tells you the problem

requires android.permission.READ_EXTERNAL_STORAGE

You have to request permission at runtime for Android API 23+ (Android 6.0+). More on this here: https://developer.android.com/training/permissions/requesting.html

if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

    requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
        REQUEST_CODE);
}

You will get the answer in onRequestPermissionsResult.