You need to call a broadcast to tell Android OS that you have added an Image or Video
So after download image/video just call below method will show your image/video in to gallery.
public void scanMedia(File f, String type) {
if (SDK_INT >= 19) {
if (type.equals("video/*")) {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, f.getAbsolutePath());
values.put(MediaStore.Video.Media.MIME_TYPE, type);
values.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis());
try{
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(context, Uri.parse(f.getAbsolutePath()));
String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
long timeInMillisec = Long.parseLong(time);
values.put(MediaStore.Video.Media.DURATION, timeInMillisec);
retriever.release();
}catch (Exception e){
values.put(MediaStore.Video.Media.DURATION, 0);
}
context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}else if(type.equals("image/*")){
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, f.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, type);
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
} else {
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(f)));
}
}
Happy Coding.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…