Amirhossein Naghshzan
Amirhossein Naghshzan

Reputation: 1180

Getting all Android device images

I need to get all images of my android device based on date they are added, so I tried below code:

String[] projection = new String[]{
    MediaStore.Images.ImageColumns._ID,
    MediaStore.Images.ImageColumns.DATA,
    MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
    MediaStore.Images.ImageColumns.DATE_TAKEN,
    MediaStore.Images.ImageColumns.MIME_TYPE};
mImageCursor = getContentResolver().query(
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    projection,
    null,
    null,
    MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
mImageCursor.moveToFirst();

It works well on most devices but the problem appears on Nexus 5, I heard it crashes. Unfortunately I Do not have Nexus 5 to check the log but I think it is probably because Nexus 5 has no sd card, Am I right? What is the solution?

Upvotes: 1

Views: 142

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006539

Starting with Android 5.0, you need READ_EXTERNAL_STORAGE for some MediaStore operations, and starting with Android 6.0, if your targetSdkVersion is 23 or higher, that requires that you work with the runtime permission system. Specifically, you would need to use requestPermissions() to prompt the user to grant you the ability to read from external storage, as that is considered to be a dangerous permission.

If your targetSdkVersion is 22 or lower, and the Android 6.0 user manually revoked your READ_EXTERNAL_STORAGE permission via the Settings app, I'm not quite sure what the behavior is with respect to MediaStore, as I haven't tried that particular scenario yet.

Upvotes: 1

Related Questions