简体   繁体   English

如何在不使用管理外部存储权限的情况下在 android 11 范围存储中获取手机存储中可用的所有 pdf 文件

[英]how to get all pdf file available in my phone storage in android 11 scope storage without using manage external storage permission

I am trying to this code.我正在尝试使用此代码。 Can someone tell me how can I get only .pdf files in my phone storage?有人能告诉我如何才能在手机存储中只获取 .pdf 文件吗?

    protected ArrayList<String> getPdfList() {
    ArrayList<String> pdfList = new ArrayList<>();
    Uri collection;

    final String[] projection = new String[]{
            MediaStore.Files.FileColumns.DISPLAY_NAME,
            MediaStore.Files.FileColumns.DATE_ADDED,
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.MIME_TYPE,
    };

    final String sortOrder = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";

    final String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ?";

    final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
    final String[] selectionArgs = new String[]{mimeType};

    if (SDK_INT >= Build.VERSION_CODES.Q) {
        collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
    } else {
        collection = MediaStore.Files.getContentUri("external");
    }


    try (Cursor cursor = getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder)) {
        assert cursor != null;

        if (cursor.moveToFirst()) {
            int columnData = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
            int columnName = cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME);
            do {
                if (new File(cursor.getString(columnData)).exists()) {
                    pdfList.add((cursor.getString(columnData)));
                    Log.d(TAG, "getPdf: " + cursor.getString(columnData));
                    //you can get your pdf files
                }
            } while (cursor.moveToNext());
        }
    }
    return pdfList;
}

above code work fine upto android 10 with requestLegacyExternalStorage = true but in android 11 cursor.moveToFirst() geting false上面的代码在 requestLegacyExternalStorage = true 下可以正常工作到 android 10 但在 android 11 cursor.moveToFirst() 中得到 false

Add these two line to your manifest:将这两行添加到您的清单中:

  <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
    tools:ignore="ScopedStorage" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想从外部存储中获取所有文档文件,但在 android 11 中没有管理存储权限 - i want to get all documents file from external storage with out manage storage permission in android 11 如何在不使用“MANAGE_EXTERNAL_STORAGE”权限的情况下删除音频文件 Android 11+ - How to delete audio file without using "MANAGE_EXTERNAL_STORAGE" permission Android 11+ 如何在不使用 MANAGE_EXTERNAL_STORAGE 的情况下读取 Android 11 中的 my.xml 文件? - How can I read my .xml file in Android 11 without using MANAGE_EXTERNAL_STORAGE? 在 Android 11+ 上自动加载没有 MANAGE_EXTERNAL_STORAGE 权限的字幕 - Auto load subtitles without MANAGE_EXTERNAL_STORAGE permission on Android 11+ 如何在Android外部存储上提供新文件时获取通知 - how to get notify when new file is available on android external storage 在 android 工作室中找不到权限 MANAGE_EXTERNAL_STORAGE - not find Permission MANAGE_EXTERNAL_STORAGE in android studio MANAGE_EXTERNAL_STORAGE 不适用于 android 11 设备 - MANAGE_EXTERNAL_STORAGE doesn't work for android 11 device 如何在 android 11 上使用没有 READ_EXTERNAL_STORAGE 的 File("/sdcard/myfile")? - How use File("/sdcard/myfile") without READ_EXTERNAL_STORAGE on android 11? Android 11, Java | 外部存储权限@SDK &gt;29 - Android 11, Java | External Storage permission @SDK >29 如何获取android设备中存储的pdf文件的信息并显示在屏幕上? - How to get the information of pdf file on storage in android device and display it on to the screen?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM