如何使用Android中的MediaStore.Files从内部和外部存储中获取所有.pdf文件

时间:2017-08-30 12:49:01

标签: android pdf android-contentprovider mediastore

  private List<PDFFileDetails> getPdfFilesFromDevice(Context context) {
    List<PDFFileDetails> listOfDirectories = new ArrayList<>();
    ContentResolver cr = context.getContentResolver();
    Uri uriExternal = MediaStore.Files.getContentUri("external");
    Uri uriInternal = MediaStore.Files.getContentUri("internal");

    String[] projection = null;

    String sortOrder = null; // unordered

    // only pdf
    String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
    String[] selectionArgsPdf = new String[]{mimeType};

这里我从内部存储器获取所有pdf,而不是从外部SD卡获取 首先,我尝试没有SD卡,之后我插入了SD卡,但没有结果来自SD卡

    Cursor allPdfFilesCursor = cr.query(uriExternal, projection, selectionMimeType, selectionArgsPdf, sortOrder);
    if (allPdfFilesCursor != null && allPdfFilesCursor.getCount() != 0) {
        allPdfFilesCursor.moveToFirst();
        do {
            int dataColumn = allPdfFilesCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            String filePath=allPdfFilesCursor.getString(dataColumn);              
        } while (allPdfFilesCursor.moveToNext());
    }

没有来自内部或外部存储的结果

    allPdfFilesCursor = cr.query(uriInternal, projection, selectionMimeType, selectionArgsPdf, sortOrder);
    if (allPdfFilesCursor != null && allPdfFilesCursor.getCount() != 0) {
        allPdfFilesCursor.moveToFirst();
        do {
            int dataColumn = allPdfFilesCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            String filePath=allPdfFilesCursor.getString(dataColumn);
        } while (allPdfFilesCursor.moveToNext());
    }

    if (allPdfFilesCursor != null && !allPdfFilesCursor.isClosed()) {
        allPdfFilesCursor.close();
    }

    return listOfDirectories;
}

1 个答案:

答案 0 :(得分:1)

可能已经很晚了,但我终于得到了解决方案,因为你要求多个文件扩展名,如.doc,pdf,xls。

 String pdf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
    String doc = MimeTypeMap.getSingleton().getMimeTypeFromExtension("doc");
    String docx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("docx");
    String xls = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xls");
    String xlsx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("xlsx");
    String ppt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("ppt");
    String pptx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pptx");
    String txt = MimeTypeMap.getSingleton().getMimeTypeFromExtension("txt");
    String rtx = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtx");
    String rtf = MimeTypeMap.getSingleton().getMimeTypeFromExtension("rtf");
    String html = MimeTypeMap.getSingleton().getMimeTypeFromExtension("html");

    //Table
    Uri table = MediaStore.Files.getContentUri("external");
    //Column
    String[] column = {MediaStore.Files.FileColumns.DATA};
    //Where
    String where = MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?"
            +" OR " +MediaStore.Files.FileColumns.MIME_TYPE + "=?";
    //args
    String[] args = new String[]{pdf,doc,docx,xls,xlsx,ppt,pptx,txt,rtx,rtf,html};

    Cursor fileCursor = getActivity().getContentResolver().query(table, column, where, args, null);

    while (fileCursor.moveToNext()) {

    //your code 


    }