7

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 :

a busy cat

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
lukas
  • 495
  • 4
  • 13

3 Answers3

10

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

jimmithy
  • 6,360
  • 2
  • 31
  • 29
  • Thank for helping. If I understand you correct, your solution creates a new folder and dont save the images in the folder like the normal camera app do? I want them in the same folder like the picures created by camera – lukas Feb 14 '14 at 11:47
  • 1
    Just remove the MediaColumns.DATA entry from the ContentValues because it specified the location of the image. Next save the image to the location returned by getContentResolver().insert(). If you take a look at the second link I provided (insertImage) it provides an example of how this is done. – jimmithy Feb 14 '14 at 14:59
  • Thank you really much! now it works as I want. Thank you for your help – lukas Feb 17 '14 at 18:08
  • Glad to be of help :) Don't forget to mark this answer as correct for others looking for the same answer to this question. – jimmithy Feb 18 '14 at 14:48
  • @jimmithy I have a bitmap, not a file path, then how can i store it using the above mentioned method. – Ali Ansari Sep 18 '14 at 06:13
  • @AliAnsari check out my reply to lukas above. Remove the DATA column, and save the bitmap to the filepath returned by insert(). The second link shows you how to do it. – jimmithy Sep 18 '14 at 07:03
  • The date only shows up after restarting the device, and it's not the one I set as an experiment, but the time it was added.. – clwhisk May 07 '15 at 15:58
  • should have shown the solution on how to save using your method. – Gustavo Baiocchi Costa Feb 20 '20 at 14:39
0

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

Alessandro Roaro
  • 4,665
  • 6
  • 29
  • 48
0

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.

Community
  • 1
  • 1
clwhisk
  • 1,805
  • 1
  • 18
  • 17