Android: Save Bitmap to Gallery ==> Time created wrong

∥☆過路亽.° 提交于 2019-12-07 03:33:53

问题


In my Android App i want to save a Bitmap in the Gallery, actually that works fine with the code below. The only mistake is that when I open the image in the gallery the time created in the details is wrong. and folowing that, the image is not in the correct order in the gallery.

Has somebody an idea? Thanks a lot for help

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
Bitmap combination = //get my bitmap!
//save in gallery
MediaStore.Images.Media.insertImage(exploreActivity.getContentResolver(),combination,"test_"+ timeStamp + ".jpg",timeStamp.toString());

here a printscreen of the details :


回答1:


You need to define DATE_TAKEN when inserting the image. This can be done by altering the way you add images to the gallery, and doing something like the following:

public static Uri addImageToGallery(Context context, String filepath, String title, String description) {    
    ContentValues values = new ContentValues();
    values.put(Media.TITLE, title);
    values.put(Media.DESCRIPTION, description); 
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.MediaColumns.DATA, filepath);

    return context.getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
}

If you need any other pointers, I would take a look at MediaStore.Images.Media.insertImage




回答2:


Have you imported correctly the class java.util.Date? Maybe the auto-import took the one from sql, common mistake.




回答3:


For those who come after me: the system will generate a stamp for the time you added the image with insertImage(...) but only after a restart or other refresh of the gallery. Follow the approach of Get filename and path from URI from mediastore combined with https://stackoverflow.com/a/5814533/2563422 to notify the gallery immediately each bitmap you store.



来源:https://stackoverflow.com/questions/21759476/android-save-bitmap-to-gallery-time-created-wrong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!