[英]Android 9 (Pie) ContentResolver query MediaStore.Images.Media.EXTERNAL_CONTENT_URI returns null on api 28
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).
下面的代码在所有设备上都可以正常工作,直到 android O (api 27)。 But it is not working on android Pie devices (api 28).
但它不适用于 android Pie 设备(api 28)。
Cursor
returns null for the query
Cursor
为query
返回 null
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步骤1.本地存储目录路径
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
Step 2. For images, Videos, Audios步骤 2. 对于图像、视频、音频
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步骤 3. 我在后台线程中调用此方法
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.当然,您可以使用更多过滤器来查找不同的文件,例如
.pdf
、 .docx
等。
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.