Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 606
lukas Avatar asked Feb 13 '14 16:02

lukas


1 Answers

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

like image 137
jimmithy Avatar answered Oct 18 '22 01:10

jimmithy