简体   繁体   中英

Android: Cursor doesn't return all videos saved in app folder in external storage

I'm currently developing video app and can save videos to app folder successfully. But when I want to display app videos in recyclerview, it doesn't display all videos (as I debug app, the cursor doesn't return all videos, and when I restart device, it returns all videos, I have to restart device if new video is added in app folder to see all videos)

Here's source code.

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;


}

1.Actually it's the Android Nature. When you add some New files to External Storage it will take sometime to Add it to Media Content Provider.Androdid System will automatically runs a mediascan in particular period.There is a workaround for that you need to trigger the Android System for a media scan

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

2.When you restart the device it will scan for the media and you will get all the videos

 I have to restart device if new video is added in app folder to see all videos

Yes. That is normal behavior. As after restart the MediaSotore will have indexed all mediafiles on your device. If you add a file the mediastore does not know aboutt that file.

So you have to tell the mediastore (with a few lines of code) that there is a new file to be indexed every time you created a new file.

Google for mediascanner and new file as code has been posted many times here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM