【问题标题】:How do I list all file in a directory in android 10 using Android Studio?如何使用 Android Studio 列出 android 10 目录中的所有文件?
【发布时间】:2020-06-29 04:13:47
【问题描述】:

我当前的代码使用:

String DirectoryPath = "/storage/emulated/0";
File f = new File(DirectoryPath);
File[] file = f.listFiles();

问题是数组对于外部的任何内容都显示为空白:

/storage/emulated/0/Android/data/com.example.myProgram

从我在线阅读的内容来看,这不再适用于 android 10+。那么如何列出某个目录中的所有文件呢? (将文件资源管理器作为 App 的一部分)

【问题讨论】:

  • 我也遇到了同样的问题,你解决了吗?
  • 这个答案帮助我在 android 10 上解决了这个问题:stackoverflow.com/a/63177725/6548239
  • 在下面查看@Johnny 的答案。

标签: java android file android-studio android-10.0


【解决方案1】:

只需将这行代码添加到应用程序标记中的清单中即可。

android:requestLegacyExternalStorage="true"

【讨论】:

【解决方案2】:

是的,使用旧代码将无法正常工作。在这里我找到了解决方案

    List<Uri> filesList = new ArrayList<>();

    Uri collection;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL);
    } else {
        collection = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    }

    String[] projection = new String[]{
            MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DISPLAY_NAME,
            MediaStore.Images.Media.SIZE
    };
    String selection = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
        selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME +
                " =? ";
    }
    String[] selectionArgs = new String[]{
            "Your folder name"
    };
    String sortOrder = MediaStore.Images.Media.DISPLAY_NAME + " ASC";
    Cursor cursor = getApplicationContext().getContentResolver().query(
            collection,
            projection,
            selection,
            selectionArgs,
            sortOrder
    );
    if (cursor != null && cursor.moveToFirst()) {
        Log.d("continue", "not null");
    } else {
        Log.d("continue", "null return");
        return;
    }
    // Cache column indices.
    int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int nameColumn =
            cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
    int sizeColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.SIZE);

    do {
        // Get values of columns for a given Images.
        long id = cursor.getLong(idColumn);
        String name = cursor.getString(nameColumn);
        int size = cursor.getInt(sizeColumn);

        Uri contentUri = ContentUris.withAppendedId(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        Log.d("continue", "path-->" + contentUri);
        filesList.add(contentUri);

        // Stores column values and the contentUri in a local object
        // that represents the media file.
    } while (cursor.moveToNext());

在上面的代码中,我只为图像编写了如果你想要任何其他文件音频视频你需要用音频和视频替换图像......等等

注意:如果将 Uri 转换为文件或字符串,则不能使用这些文件。你会在 android 10 中得到一个错误

【讨论】:

    【解决方案3】:

    要获取文件名,试试这个。

    File sdCardRoot = Environment.getExternalStorageDirectory();
    File yourDir = new File(sdCardRoot, "path");
    for (File f : yourDir.listFiles()) {
        if (f.isFile())
            String name = f.getName();
            // Do your stuff
    }
    

    【讨论】:

    • 我收到错误消息:“getExternalStorageDirectory()”自 API 29 起已弃用:Android 10.0 (Q)
    • 如果您收到一条消息说某些东西已被弃用,那么这不是错误。
    • 是的,它已被弃用,即使使用 legacyStorage 也不会在 Android 11+ 中可用。
    【解决方案4】:

    你所描述的是有效的。

    使用存储访问框架能够列出所有目录。

    【讨论】:

      【解决方案5】:

      基于https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory(),为了提高用户隐私,不推荐直接访问共享/外部存储设备应用程序可以通过迁移到诸如 Context#getExternalFilesDir(String)、MediaStore 等替代方案来继续访问存储在共享/外部存储上的内容, 或 Intent#ACTION_OPEN_DOCUMENT。

      就我而言,我需要在用户设备上查找图像或任何类型的文档,例如 .docx、.xls 文件,因此我使用如下所示的 ACTION_OPEN_DOCUMENT:

           Intent intent = new Intent();
              intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
              intent.setType("*/*");
              startActivityForResult(intent, REQUEST_CODE_INTENT_GET_CV);
      

      【讨论】:

        猜你喜欢
        • 2015-06-13
        • 2012-01-28
        • 2015-03-15
        • 1970-01-01
        • 2011-04-15
        • 2011-03-13
        • 2014-03-08
        相关资源
        最近更新 更多