首页 > 解决方案 > 获取存储中的 pdf 文件列表 - Mediastore android

问题描述

有人可以帮我解决这个问题吗,我想从外部存储中获取 pdf 文件列表,但数组总是空的,这是我的代码:

先感谢您。

这是我的代码:

public static ArrayList<String> getPdfFiles(Context context) {
        ArrayList<String> listOfAllFiles = new ArrayList<>();

        Uri uri = MediaStore.Files.getContentUri("external");
        ContentResolver cr = context.getContentResolver();
        String[] projection = {MediaStore.MediaColumns.DATA};
        // only pdf
        String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
        String[] selectionArgsPdf = new String[]{ mimeType };
        Cursor allPdfFiles = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, null);

        while (allPdfFiles.moveToNext()) {
            int nameIndex= allPdfFiles.getColumnIndex(MediaStore.Files.FileColumns.TITLE);
            String name= allPdfFiles.getString(nameIndex);
            listOfAllFiles.add(name);
        }
        return listOfAllFiles;
    }

标签: javaandroidpdfmobilemediastore

解决方案


推荐阅读