繁体   English   中英

Android 9 (Pie) ContentResolver 查询 MediaStore.Images.Media.EXTERNAL_CONTENT_URI 在 api 28 上返回 null

[英]Android 9 (Pie) ContentResolver query MediaStore.Images.Media.EXTERNAL_CONTENT_URI returns null on api 28

我正在获取设备中所有图像、视频和音频文件的列表。 下面的代码在所有设备上都可以正常工作,直到 android O (api 27)。 但它不适用于 android Pie 设备(api 28)。 Cursorquery返回 null

对于图像

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");

对于视频文件

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");

对于音频文件

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");

请如果有人可以帮助我!

因此,我能够为此找到解决方法。 如果有人找到更好的方法,请告诉我。

步骤1.本地存储目录路径

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());

步骤 2. 对于图像、视频、音频

private ArrayList<File> allShownImagesPath = new ArrayList<>();
private ArrayList<File> allShownVideoPath = new ArrayList<>();
private ArrayList<File> allShownAudioPath = new ArrayList<>();

步骤 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);
                        }
                    }
                }
            }
        }
    }

当然,您可以使用更多过滤器来查找不同的文件,例如.pdf.docx等。

与问题无关,但您可以对文件进行排序。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM