![](/img/trans.png)
[英]MediaStore cursor not displaying non-media files in Android 10
[英]MediaStore - Uri to query all types of files (media and non-media)
在類MediaStore.Files類中,它提到了,
媒體提供程序表,包含媒體存儲中所有文件的索引,包括非媒體文件。
我有興趣查詢像PDF這樣的非媒體文件。
我正在使用CursorLoader來查詢數據庫。 構造函數的第二個參數需要一個Uri參數,很容易獲得媒體類型音頻,圖像和視頻,因為每個參數都為它們定義了EXTERNAL_CONTENT_URI
和INTERNAL_CONTENT_URI
常量。
對於MediaStore.Files,沒有這樣定義的常量。 我嘗試使用getContentUri()
方法,但無法找出volumeName
的參數值。 我嘗試給出“/ mnt / sdcard”以及將設備連接到我的系統時出現的卷名但是徒勞無功。
我在Google網上論壇上看到了類似的問題但尚未解決。
編輯:我也嘗試使用Uri.fromFile(新文件(“/ mnt / sdcard /”))和Uri.parse(新文件(“/ mnt / sdcard”)。toString()),但這也沒有用。
雖然內部(系統文件)在這里可能沒用,但它是"external"
或"internal"
。
ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;
// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null; // there is no ? in selection so null here
String sortOrder = null; // unordered
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
如果你想要.pdf
你只能檢查mimetype
// 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, sortOrder);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.