0

I want to make a lib like a photo album and adapt it into Android Q

Because of the Scoped Storage, the MediaStore.Images.ImageColumns.DATA was deprecated;

We can't read the file directly by the path like /storage/emulated/0/DCIM/xxx.png

MediaStore.Images.ImageColumns has no value like URI, So I can't get the picture by ContentProvider.

We can open only one picture in this way (the code under), and received one URI in the callback;

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones).
intent.addCategory(Intent.CATEGORY_OPENABLE);
// Filter to show only text files.
intent.setType("image/*");

But I want to access all the picture, So, How can I scan all the pic in Android Q?

Kuya
  • 7,280
  • 4
  • 19
  • 31
weechan
  • 87
  • 9
  • 1
    You've never needed the `DATA` column to access the bytes of the image. Calling [ContentResolver.openInputStream()](https://developer.android.com/reference/android/content/ContentResolver#openInputStream(android.net.Uri)) on the URI is supported back to API 1. What makes you think you need the `DATA` column? – ianhanniballake Jul 19 '19 at 03:40

1 Answers1

6

This is how I've always retrieved all images without needing the MediaStore.MediaColumns.DATA constant

val externalUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

val cursor = context.contentResolver
                .query(
                    externalUri,
                    arrayOf(MediaStore.MediaColumns._ID, MediaStore.MediaColumns.DATE_MODIFIED),
                    null,
                    null,
                    "${MediaStore.MediaColumns.DATE_MODIFIED} DESC"
                )

if(cursor != null){
    while(cursor.moveToNext()){
        val id = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns._ID))
        val uri = ContentUris.withAppendedId(externalUri, id.toLong())

        //do whatever you need with the uri
    }
}

cursor?.close()

It is written in Kotlin but if it shouldn't be hard to convert to java

Leo
  • 14,625
  • 2
  • 37
  • 55
  • Can you help me with https://stackoverflow.com/questions/63543414/rename-file-of-the-external-storage-which-is-created-by-app-in-android-10-worki – jazzbpn Aug 24 '20 at 03:42