I am fetching list of all the images, videos and audio files in the device. The below code is working fine on all devices up till android O (api 27). But it is not working on android Pie devices (api 28). Cursor
returns null for the query
For Images
String[] proj = {MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.SIZE
};
ContentResolver cr = context.getContentResolver();
Cursor imagescursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj, null, null, "_size DESC");
For Video files
String[] proj = {MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA,
MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.SIZE,
MediaStore.Video.Media.DURATION
};
ContentResolver cr = context.getContentResolver();
Cursor videocursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
proj, null, null, "_size DESC");
For Audio files
String[] proj = {MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.SIZE,
MediaStore.Audio.Media.DURATION};
ContentResolver cr = context.getContentResolver();
Cursor audiocursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj, null, null, "_size DESC");
Please if anyone can help me out!
So I am able to find a workaround for this. Please let me know if anyone finds a better way to do it.
Step 1. local storage directory path
Step 2. For images, Videos, Audios
Step 3. I am calling this method in background thread
Of course you can use more filters to find different files like
.pdf
,.docx
, etc.Not relevant to the question, but you can sort your files.