Java 安卓:光标不';t返回保存在外部存储中的应用程序文件夹中的所有视频

Java 安卓:光标不';t返回保存在外部存储中的应用程序文件夹中的所有视频,java,android,video,Java,Android,Video,我目前正在开发视频应用程序,可以将视频成功保存到应用程序文件夹。 但当我想在recyclerview中显示应用程序视频时,它不会显示所有视频(当我调试应用程序时,光标不会返回所有视频,当我重新启动设备时,它会返回所有视频,如果在应用程序文件夹中添加新视频以查看所有视频,我必须重新启动设备) 下面是源代码 public static List<MyVideo> getAppVideos(Context context) { String[] projection = { Med

我目前正在开发视频应用程序,可以将视频成功保存到应用程序文件夹。 但当我想在recyclerview中显示应用程序视频时,它不会显示所有视频(当我调试应用程序时,光标不会返回所有视频,当我重新启动设备时,它会返回所有视频,如果在应用程序文件夹中添加新视频以查看所有视频,我必须重新启动设备)

下面是源代码

public static List<MyVideo> getAppVideos(Context context) {
    String[] projection = { MediaStore.Video.Media.DATA, MediaStore.Video.Media.DATE_TAKEN, MediaStore.Video.Media.DURATION};
    String selection = MediaStore.Images.Media.BUCKET_ID + " = ?";
    String[] selectionArgs = { APP_VIDEO_BUCKET_ID };
    Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            projection,
            selection,
            selectionArgs,
            MediaStore.Files.FileColumns.DATE_ADDED + " DESC");
    ArrayList<MyVideo> result = new ArrayList<MyVideo>(cursor.getCount());
    if (cursor.moveToFirst()) {
        final int dataColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        final int dateColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_TAKEN);
        final int durationColumn = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
        do {
            final String data = cursor.getString(dataColumn);
            final String createdDate = cursor.getString(dateColumn);
            final String durationTime = cursor.getString(durationColumn);
            result.add(new MyVideo(data, DateUtils.convertToSeconds(durationTime), "^ " +  DateUtils.getDateFromMilliSeconds(Long.parseLong(createdDate), "MMM dd, yyyy")));

        } while (cursor.moveToNext());
    }
    cursor.close();
    return result;


}
公共静态列表getAppVideos(上下文){
字符串[]投影={MediaStore.Video.Media.DATA,MediaStore.Video.Media.DATE_take,MediaStore.Video.Media.DURATION};
字符串选择=MediaStore.Images.Media.BUCKET_ID+“=?”;
字符串[]selectionArgs={APP\u VIDEO\u BUCKET\u ID};
Cursor Cursor=context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL\u CONTENT\u URI,
投影,
选择,
精选,
MediaStore.Files.FileColumns.DATE_添加+“DESC”);
ArrayList结果=新的ArrayList(cursor.getCount());
if(cursor.moveToFirst()){
final int dataColumn=cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
final int dateColumn=cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATE_take);
final int durationColumn=cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION);
做{
最终字符串数据=cursor.getString(dataColumn);
最终字符串createdDate=cursor.getString(dateColumn);
最终字符串durationTime=cursor.getString(durationColumn);
结果.添加(新的MyVideo(数据,DateUtils.convertToSeconds(durationTime),“^”+DateUtils.GetDateFrom毫秒(Long.parseLong(createdDate),“MMM dd,yyyy”));
}while(cursor.moveToNext());
}
cursor.close();
返回结果;
}

1.实际上这是安卓的天性。 当您将一些新文件添加到外部存储时,需要一些时间才能将其添加到媒体内容提供商。Androdid系统将在特定时间段自动运行mediascan。有一种解决方法,您需要触发Android系统进行媒体扫描

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(videoAdded)));

2.当您重新启动设备时,它将扫描媒体,您将获得所有视频。实际上,这是安卓系统的本质。
 I have to restart device if new video is added in app folder to see all videos
当您将一些新文件添加到外部存储时,需要一些时间才能将其添加到媒体内容提供商。Androdid系统将在特定时间段自动运行mediascan。有一种解决方法,您需要触发Android系统进行媒体扫描

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(videoAdded)));
2.当您重新启动设备时,它将扫描媒体,您将获得所有视频

 I have to restart device if new video is added in app folder to see all videos
对。这是正常的行为。重新启动后,MediaSotore将为设备上的所有媒体文件编制索引。如果您添加一个文件,mediastore不知道该文件

因此,每次创建新文件时,都必须告诉mediastore(只需几行代码)有一个新文件要编制索引

谷歌的mediascanner和新文件作为代码已经在这里发布了很多次

对。这是正常的行为。重新启动后,MediaSotore将为设备上的所有媒体文件编制索引。如果您添加一个文件,mediastore不知道该文件

因此,每次创建新文件时,都必须告诉mediastore(只需几行代码)有一个新文件要编制索引


Google for mediascanner和new file as code已在此处发布多次。

我检查了您的ans,但不知道您的问题…(:抱歉,您的意思是什么?),即使应用文件夹中有5个视频,光标仍返回0计数。我检查了您的ans,但不知道您的问题。。。(:抱歉,但你的意思是什么?),即使应用程序文件夹中有5个视频,光标仍返回0计数。谢谢,我从你的答案中找到了解决方案。谢谢,我从你的答案中找到了解决方案。谷歌搜索了mediascanner并了解了它是什么,:)谢谢。谷歌搜索了mediascanner并了解了它是什么,:)谢谢。