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

huangapple go评论82阅读模式
英文:

Unable to get all Video files from storage Android

问题

以下是翻译好的部分:

我正在使用以下方法从存储中获取视频文件。它仅检索目录文件,而不读取子目录文件内部的文件。

示例: 我可以从“WhatsApp视频”中读取文件,但在同一文件夹中有一个名为“sent”的子文件夹,我无法读取其中的文件。

有任何帮助吗?如何读取所有文件夹和子文件夹?

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

我应该怎么做才能获取存储中的所有视频文件?

奖励金
我想从存储中读取每个视频文件
使用上述代码,我只能读取MediaStore.Video限定位置的文件。

英文:

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.

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&lt;VideoViewInfo&gt; 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(&quot;Recycle&quot;)
Cursor cur = context.getContentResolver().query(Video, projection,null,null,orderBy );
assert cur != null;
Log.i(&quot;ListingVideo&quot;,&quot; query count=&quot; + 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, &quot;getAllVideosData: &quot;+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

答案1

得分: 3

你可以运行一个递归函数来列出所有的视频文件,如下所示:

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

或者你可以像这个仓库中所做的那样查询媒体存储,具体代码请参见此处链接:这里

英文:

You can run a recursive function to list all video files like

void findVideos(File dir, ArrayList&lt;String&gt; list){
    for (File file : dir.listFiles()) {
        if (file.isDirectory()) findVideos(file, list);
        else if(file.getAbsolutePath().contains(&quot;.mp4&quot;)) list.add(file.getAbsolutePath());
    }
}

Or you can query the mediastore like done in this repository here

答案2

得分: 0

处理文件时非常困难且充满混乱,因为你只能访问文件,但其中的子目录呢?对于@Kursh的回答来说,递归也无济于事,因为它只会深入子目录,永远不会返回。我曾经开发过算法,但效率确实不高,不过你可以使用这个库,并记得在ASYNC TAKCoroutine中使用它。

英文:

It&#39;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? well in the case of

@Kursh's answer recursion will not help either

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

huangapple
  • 本文由 发表于 2020年9月22日 19:48:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009121.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定