[英]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.