I am looking to read a set of image files from the sdcard/download
directory. I am getting content://media/external_primary/downloads
as the URI string when calling
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
collection = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
}
I am using the Cursor
interface to iterate through the directory but for some reason, my cursor is returning with a size
of 0
. I do not know what I am doing wrong to make this happen but below is the code that I have so far. Looking for some direction.
Uri collection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
collection = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
}
String[] projection = {
MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.TITLE
};
try {
Cursor cursor = getApplicationContext().getContentResolver().query(collection, projection, null, null, null);
// Cache column indices.
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID);
int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads.TITLE);
while (cursor.moveToNext()) {
// Get values of columns for a given video.
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DISPLAY_NAME));
Uri contentUri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, id);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageURI(contentUri);
}
} catch (Exception e) {
e.printStackTrace();
}
Snippet to the directory structure