Reputation: 564
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!
Upvotes: 5
Views: 2628
Reputation: 564
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
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
Step 2. For images, Videos, Audios
private ArrayList<File> allShownImagesPath = new ArrayList<>();
private ArrayList<File> allShownVideoPath = new ArrayList<>();
private ArrayList<File> allShownAudioPath = new ArrayList<>();
Step 3. I am calling this method in background thread
public void searchDir(File dir) {
//audio
String mp3 = ".mp3";
String ogg = ".ogg";
String aac = ".aac";
//video
String mp4 = ".mp4";
String mkv = ".mkv";
String avi = ".avi";
String webm = ".webm";
//images
String webp = ".webp";
String png = ".png";
String jpg = ".jpg";
String gif = ".gif";
File[] listFile = dir.listFiles();
if (listFile != null) {
for (File file : listFile) {
if (file.isDirectory()) {
if (!file.getName().equals("Android") && !file.isHidden()) // will skip Android folder, hidden folders
searchDir(file);
} else {
if (!file.isHidden()) { // will skip hidden files, totally up to you
String name = file.getName();
if (name.endsWith(mp3) || name.endsWith(ogg) || name.endsWith(aac)) { //Audio file
allShownAudioPath.add(file);
} else if (name.endsWith(mp4) || name.endsWith(mkv) || name.endsWith(avi) || name.endsWith(webm)) { //Video file
allShownVideoPath.add(file);
} else if (name.endsWith(webp) || name.endsWith(png) || name.endsWith(jpg) || name.endsWith(gif)) { //Image file
allShownImagesPath.add(file);
}
}
}
}
}
}
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.
if (allShownImagesPath.size() > 0){
File[] sortedImgFiles = allShownImagesPath.toArray(new File[0]);
Arrays.sort(sortedImgFiles, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
}
Upvotes: 2