如何使用 Android Studio 列出 android 10 目录中的所有文件? [英] How do I list all file in a directory in android 10 using Android Studio?

查看:30
本文介绍了如何使用 Android Studio 列出 android 10 目录中的所有文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前的代码使用:

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

问题是数组对于外面的任何东西都显示为空白:

The problem is that the array comes up as blank for anything outside:

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

根据我在网上阅读的内容,这不再适用于 android 10+.那么如何列出某个目录中的所有文件呢?(将文件浏览器作为应用程序的一部分)

From what I read online this no longer works with android 10+. So how would I list all files in a certain directory? (Making a file explorer as part of an App)

推荐答案

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

Yes, With old code it won't work. Here i found the solution

    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());

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

In the above code, I written for images only if you want any other files audio video you need to replace Images with Audio and Video ...etc

注意:如果将 Uri 转换为文件或字符串,则无法使用这些文件.android 10 会报错

这篇关于如何使用 Android Studio 列出 android 10 目录中的所有文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆