简体   繁体   English

无法从存储中获取所有视频文件 Android

[英]Unable to get all Video files from storage Android

Am using following method to get video files from storage.我正在使用以下方法从存储中获取视频文件。 it retrieves only directories files but not read files inside sub-directory files.它只检索目录文件,但不读取子目录文件中的文件。

Example:- i can read file from WhatsApp Video but in the same folder there is a sub folder name sent i can't read the files inside.示例:-我可以从WhatsApp Video读取文件,但在同一文件夹中有一个子文件夹名称已sent ,我无法读取其中的文件。

any help, how can i read all folder and sub-folder任何帮助,我怎样才能阅读所有文件夹和子文件夹

public class GetVideos {

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void getAllVideosData(Context context, ArrayList<VideoViewInfo> AllVideosData) {

        String[] projection = new String[] {
                MediaStore.Video.Media._ID,
                MediaStore.Video.Media.TITLE,
                MediaStore.Video.VideoColumns.DATA,
                MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
                MediaStore.Video.Media.DATE_TAKEN,
                MediaStore.Video.Media.DURATION,
                MediaStore.Video.Media.RESOLUTION,
                MediaStore.Video.Media.SIZE
    };

        Uri Video = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        String orderBy = MediaStore.Video.Media.DATE_TAKEN;

        @SuppressLint("Recycle")
        Cursor cur = context.getContentResolver().query(Video, projection,null,null,orderBy );
        assert cur != null;
        Log.i("ListingVideo"," query count=" + cur.getCount());

        if (cur.moveToNext()) {
            String id;
            String title;
            String filePath;
            String bucketName;
            String date;
            String duration;
            String resolution;
            String size;

            int tempId = cur.getColumnIndex(MediaStore.Video.Media._ID);
            int titleTemp = cur.getColumnIndex(MediaStore.Video.Media.TITLE);
            int filePathTemp = cur.getColumnIndex(MediaStore.Video.Media.DATA);
            int bucketTemp = cur.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
            int dateTemp = cur.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN);
            int durationTemp = cur.getColumnIndex(MediaStore.Video.Media.DURATION);
            int resolutionTemp = cur.getColumnIndex(MediaStore.Video.Media.RESOLUTION);
            int sizeTemp = cur.getColumnIndex(MediaStore.Video.Media.SIZE);
            do {
                id = cur.getString(tempId);
                title = cur.getString(titleTemp);
                filePath = cur.getString(filePathTemp);
                bucketName = cur.getString(bucketTemp);
                date = cur.getString(dateTemp);
                duration = cur.getString(durationTemp);
                resolution = cur.getString(resolutionTemp);
                size = cur.getString(sizeTemp);
                Log.d(TAG, "getAllVideosData: "+bucketName);
                VideoViewInfo vvi = new VideoViewInfo(id, title, filePath, bucketName, date, duration, resolution, size);
                AllVideosData.add(vvi);

            } while (cur.moveToNext());
        }
    }
}

What should i do to get all video file in my storage?我应该怎么做才能在我的存储中获取所有视频文件?

Bountry reward i want to read every video file from storage with above code i can only read files limited location as per MediaStore.Video赏金我想用上面的代码从存储中读取每个视频文件我只能根据MediaStore.Video读取文件有限的位置

You can run a recursive function to list all video files like您可以运行递归 function 来列出所有视频文件,例如

void findVideos(File dir, ArrayList<String> list){
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) findVideos(file, list);
        else if(file.getAbsolutePath().contains(".mp4")) list.add(file.getAbsolutePath());
    }
}

Or you can query the mediastore like done in this repository here或者您可以在此处查询媒体存储库,就像在此存储库中所做的那样

It's really hard and full of a mess when doing file handling because you only access the files but what about the subdirectories in it? It's really hard and full of a mess when doing file handling因为你只访问文件,但是其中的子目录呢? well in the case of那么在这种情况下

@Kursh's answer recursion will not help either @Kursh 的答案递归也无济于事

because it will just go deep into the sub directories never to come back I had developed the algorithm but it was not really inefficient, but you can use this library and remember to use it in an ASYNC TAK or Coroutine因为只会go深入到子目录中永远不会回来

但是递归方法不适用于获取视频文件信息

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

相关问题 从 Firebase 存储中获取所有文件 - Get all files from Firebase Storage 如何在java中从android的可移动存储(SD卡)获取所有pdf文件的链接? - How to get links to all pdf files from removable storage(SD Card) of android in java? 无法从Android手机的内部和外部存储读取文件 - Unable to read files from internal and external storage of android phone 从android上的外部存储中获取所有.pdf文件 - Getting all .pdf files from External storage on android 是否可以以编程方式从android上的照片/视频目录中删除所有文件? - is it possible to delete all files from photo/video directory on android programmatically? 无法从外部存储中获取音频文件 - Android Studio - Can't get audio files from external storage - Android Studio 如何从Android App中的设备存储中获取所有视频 - How to get all videos from device storage in Android App 在android中查找存储中的所有mp3文件 - Find all mp3 files in storage in android 我想从外部存储中获取所有文档文件,但在 android 11 中没有管理存储权限 - i want to get all documents file from external storage with out manage storage permission in android 11 在Android中使用绝对路径获取内部和外部存储中所有文件(每个文件)的列表 - Get list of all files (every file) in both internal and external storage with absolute path in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM