简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM