weechan
weechan

Reputation: 107

How to acess all the picture in Android Q?

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?

Upvotes: 0

Views: 1275

Answers (1)

Leo
Leo

Reputation: 14860

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

Upvotes: 6

Related Questions