簡體   English   中英

如何列出 android 10 上的所有 pdf 文件?

[英]How to list all pdf files on android 10?

由於更改與訪問共享存儲的授權相關,因此似乎不再可能通過這種方法搜索 pdf 類型的所有文檔(使用 requestLegacyExternalStorage = "false"):

ContentResolver cr = context.getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = null;
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
                    + MediaStore.Files.FileColumns.MEDIA_TYPE_NONE;
String[] selectionArgs = null;
String sortOrder = null;
Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);

檢查此鏈接: 媒體數據限制

我看到的唯一解決方案是使用 SAF 以遞歸方式掃描共享存儲的所有樹,這在我看來資源非常昂貴且荒謬。

有人有其他想法嗎?

范圍存儲的基本思想正是為了避免這種行為,您無法知道用戶手機中的某個位置是否存在某些文件。 您可以請求訪問存儲樹的權限並按照您所說的掃描所有內容。 即使在這種情況下,用戶也可以 select 與根目錄不同的文件夾,因此您的應用程序將被限制在該文件夾中。 這個想法可以是執行掃描,然后使用在修改樹 URI 和后代時安排的作業(作業服務)來更新您的數據庫。

如果您的項目針對 Sdk 級別 29,您應該在您的 AndroidManifest.xml 中添加標志<application> android:requestLegacyExternalStorage="true"

 public List<String> queryFilesFromDevice(Uri uri, String[] projection, String selection, final Context context) {
    final List<String> tempList = new ArrayList<>();
    Cursor c = context.getContentResolver().query(uri, projection,
            selection,
            null,
            null);
    if (c != null) {

        while (c.moveToNext()) {
            String path = c.getString(0);
            long size = c.getLong(1);
         // your code logic should be here
        }
        c.close();
    }
    tempList.add(path);
    return tempList;
}

您需要像這樣調用 function:

String pdfExt = "_data LIKE '%.pdf'";
Uri ducumentsUri = MediaStore.Files.getContentUri("external");
String[] docsProjection ={MediaStore.Files.FileColumns.DATA,MediaStore.Images.Media.SIZE,MediaStore.Files.FileColumns.MIME_TYPE,};
queryFilesFromDevice(ducumentsUri, docsProjection, pdfExt, this);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM