Android 10. MediaStore. Getting all images

I try to get all images. I use this code:

 val galleryImageUrls = mutableListOf<String>()
    val columns = arrayOf(MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID)
    val orderBy = MediaStore.Images.Media.DATE_TAKEN

    appContext.contentResolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
        null, null, "$orderBy DESC"
    )?.use { cursor ->
        while (cursor.moveToNext()) {
            galleryImageUrls.add(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)))//get Image from column index
        }
    }

This code works if I use compileSdkVersion 28, but it doesn't work on compileSdkVersion 29. Do you have any ideas how I can fix it? Or should I use storage access framework?


This code works if I use compileSdkVersion 28

It will not be reliable. There is no guarantee that DATA will hold a value that you can use.

Do you have any ideas how I can fix it?

    val galleryImageUrls = mutableListOf<Uri>()
    val columns = arrayOf(MediaStore.Images.Media._ID)
    val orderBy = MediaStore.Images.Media.DATE_TAKEN

    appContext.contentResolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
        null, null, "$orderBy DESC"
    )?.use { cursor ->
        val idColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID)

        while (cursor.moveToNext()) {
            val id = cursor.getLong(idColumn)

            galleryImageUrls.add(ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id)
        }
    }

{

    ArrayList<Photo> listPhotoPhone = new ArrayList<>();
    Cursor cursor;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        // Codigo version de api 29 en adelante

        cursor = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                new String[]{MediaStore.Images.Media._ID},
                null,
                null,
                MediaStore.Images.Media.DATE_TAKEN + " DESC"
        );

        if (null == cursor) {
            return listPhotoPhone;
        }

        if (cursor.moveToFirst()) {
            do {
                String photoUrl = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(MediaStore.Images.Media._ID))).toString();
                Photo photo = new Photo(photoUrl);
                listPhotoPhone.add(photo);

            } while (cursor.moveToNext());
        }
    } else {

        cursor = contentResolver.query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                null,
                null,
                null,
                MediaStore.Images.Media.DATA + " DESC"
        );

        if (null == cursor) {
            return listPhotoPhone;
        }

        if (cursor.moveToFirst()) {
            do {
                String fullPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
                Photo photo = new Photo(fullPath);
                listPhotoPhone.add(photo);

            } while (cursor.moveToNext());
        }

    }
    cursor.close();
    return listPhotoPhone;
}