问题
With the following code, the Android Gallery is not refreshed instantly. If i try to see the new files, I need to restart the device or wait.
private void addVideoGallery() {
File f = new File(outputfilename);
Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri fileContentUri = Uri.fromFile(f);
mediaScannerIntent.setData(fileContentUri);
this.sendBroadcast(mediaScannerIntent);
}
With this other code, all works fine but in the logcat I see an error that my Activity has leaked MediaScanner Connection:
private void addVideoGallery() {
File f = new File(outputfilename);
MediaScannerConnection.scanFile(this, new String[] { f.getPath() }, new String[] { "video/mp4" }, null);
}
回答1:
Solved with:
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, outputfilename);
ContentResolver resolver = MainActivity.this.getContentResolver();
resolver.insert(Video.Media.EXTERNAL_CONTENT_URI, values);
来源:https://stackoverflow.com/questions/24555575/android-gallery-not-instantly-refreshed-with-new-recorded-videos