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

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

Unable to get all Video files from storage Android

问题

以下是翻译好的部分:

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

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

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

  1. public class GetVideos {
  2. @RequiresApi(api = Build.VERSION_CODES.Q)
  3. public void getAllVideosData(Context context, ArrayList<VideoViewInfo> AllVideosData) {
  4. String[] projection = new String[] {
  5. MediaStore.Video.Media._ID,
  6. MediaStore.Video.Media.TITLE,
  7. MediaStore.Video.VideoColumns.DATA,
  8. MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
  9. MediaStore.Video.Media.DATE_TAKEN,
  10. MediaStore.Video.Media.DURATION,
  11. MediaStore.Video.Media.RESOLUTION,
  12. MediaStore.Video.Media.SIZE
  13. };
  14. Uri Video = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  15. String orderBy = MediaStore.Video.Media.DATE_TAKEN;
  16. @SuppressLint("Recycle")
  17. Cursor cur = context.getContentResolver().query(Video, projection,null,null,orderBy );
  18. assert cur != null;
  19. Log.i("ListingVideo"," query count=" + cur.getCount());
  20. if (cur.moveToNext()) {
  21. String id;
  22. String title;
  23. String filePath;
  24. String bucketName;
  25. String date;
  26. String duration;
  27. String resolution;
  28. String size;
  29. int tempId = cur.getColumnIndex(MediaStore.Video.Media._ID);
  30. int titleTemp = cur.getColumnIndex(MediaStore.Video.Media.TITLE);
  31. int filePathTemp = cur.getColumnIndex(MediaStore.Video.Media.DATA);
  32. int bucketTemp = cur.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
  33. int dateTemp = cur.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN);
  34. int durationTemp = cur.getColumnIndex(MediaStore.Video.Media.DURATION);
  35. int resolutionTemp = cur.getColumnIndex(MediaStore.Video.Media.RESOLUTION);
  36. int sizeTemp = cur.getColumnIndex(MediaStore.Video.Media.SIZE);
  37. do {
  38. id = cur.getString(tempId);
  39. title = cur.getString(titleTemp);
  40. filePath = cur.getString(filePathTemp);
  41. bucketName = cur.getString(bucketTemp);
  42. date = cur.getString(dateTemp);
  43. duration = cur.getString(durationTemp);
  44. resolution = cur.getString(resolutionTemp);
  45. size = cur.getString(sizeTemp);
  46. Log.d(TAG, "getAllVideosData: "+bucketName);
  47. VideoViewInfo vvi = new VideoViewInfo(id, title, filePath, bucketName, date, duration, resolution, size);
  48. AllVideosData.add(vvi);
  49. } while (cur.moveToNext());
  50. }
  51. }
  52. }

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

奖励金
我想从存储中读取每个视频文件
使用上述代码,我只能读取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

  1. public class GetVideos {
  2. @RequiresApi(api = Build.VERSION_CODES.Q)
  3. public void getAllVideosData(Context context, ArrayList&lt;VideoViewInfo&gt; AllVideosData) {
  4. String[] projection = new String[] {
  5. MediaStore.Video.Media._ID,
  6. MediaStore.Video.Media.TITLE,
  7. MediaStore.Video.VideoColumns.DATA,
  8. MediaStore.Video.Media.BUCKET_DISPLAY_NAME,
  9. MediaStore.Video.Media.DATE_TAKEN,
  10. MediaStore.Video.Media.DURATION,
  11. MediaStore.Video.Media.RESOLUTION,
  12. MediaStore.Video.Media.SIZE
  13. };
  14. Uri Video = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  15. String orderBy = MediaStore.Video.Media.DATE_TAKEN;
  16. @SuppressLint(&quot;Recycle&quot;)
  17. Cursor cur = context.getContentResolver().query(Video, projection,null,null,orderBy );
  18. assert cur != null;
  19. Log.i(&quot;ListingVideo&quot;,&quot; query count=&quot; + cur.getCount());
  20. if (cur.moveToNext()) {
  21. String id;
  22. String title;
  23. String filePath;
  24. String bucketName;
  25. String date;
  26. String duration;
  27. String resolution;
  28. String size;
  29. int tempId = cur.getColumnIndex(MediaStore.Video.Media._ID);
  30. int titleTemp = cur.getColumnIndex(MediaStore.Video.Media.TITLE);
  31. int filePathTemp = cur.getColumnIndex(MediaStore.Video.Media.DATA);
  32. int bucketTemp = cur.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
  33. int dateTemp = cur.getColumnIndex(MediaStore.Video.Media.DATE_TAKEN);
  34. int durationTemp = cur.getColumnIndex(MediaStore.Video.Media.DURATION);
  35. int resolutionTemp = cur.getColumnIndex(MediaStore.Video.Media.RESOLUTION);
  36. int sizeTemp = cur.getColumnIndex(MediaStore.Video.Media.SIZE);
  37. do {
  38. id = cur.getString(tempId);
  39. title = cur.getString(titleTemp);
  40. filePath = cur.getString(filePathTemp);
  41. bucketName = cur.getString(bucketTemp);
  42. date = cur.getString(dateTemp);
  43. duration = cur.getString(durationTemp);
  44. resolution = cur.getString(resolutionTemp);
  45. size = cur.getString(sizeTemp);
  46. Log.d(TAG, &quot;getAllVideosData: &quot;+bucketName);
  47. VideoViewInfo vvi = new VideoViewInfo(id, title, filePath, bucketName, date, duration, resolution, size);
  48. AllVideosData.add(vvi);
  49. } while (cur.moveToNext());
  50. }
  51. }
  52. }

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

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

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

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

英文:

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

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

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:

确定