1

I want to save a picture into the gallery. it works with this code below, but the name from the picture is the System.currentTimeMillis .jpg Has anybody a sugestion what I can change that the name is like my title?

/**
 * Insert an image and create a thumbnail for it.
 *
 * @param cr The content resolver to use
 * @param source The stream to use for the image
 * @param title The name of the image
 * @param description The description of the image
 * @return The URL to the newly created image, or <code>null</code> if the image failed to be stored
 * for any reason.
 */
private static final String insertImage(ContentResolver cr, Bitmap source,
        String title, String description) {
    ContentValues values = new ContentValues();
    values.put(Media.TITLE, title);
    values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
    values.put(Images.Media.MIME_TYPE, "image/jpeg");

    Uri uri = null;
    String stringUrl = null; /* value to be returned */

    try {
        uri = cr.insert(Images.Media.EXTERNAL_CONTENT_URI, values);

        if (source != null) {
            OutputStream imageOut = cr.openOutputStream(uri);

            try {
                source.compress(Bitmap.CompressFormat.JPEG, 100, imageOut);
            } finally {                 
                imageOut.close();
            }

            long id = ContentUris.parseId(uri);
            // Wait until MINI_KIND thumbnail is generated.
            Bitmap miniThumb = Images.Thumbnails.getThumbnail(cr, id,
                    Images.Thumbnails.MINI_KIND, null);
            // This is for backward compatibility.
            Bitmap microThumb = StoreThumbnail(cr, miniThumb, id, 50F, 50F,
                    Images.Thumbnails.MICRO_KIND);
        } else {
            Log.e(TAG, "Failed to create thumbnail, removing original");
            cr.delete(uri, null, null);
            uri = null;
        }
    } catch (Exception e) {
        Log.e(TAG, "Failed to insert image", e);
        if (uri != null) {
            cr.delete(uri, null, null);
            uri = null;
        }
    }

    if (uri != null) {
        stringUrl = uri.toString();
    }

    return stringUrl;
}

Edit: I call the method with this code:

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());      
    insertImage(exploreActivity.getContentResolver(), bitmap, companyName+ timeStamp + ".jpg",companyName+ timeStamp + ".jpg");

Thanks a lot for any kind of help.

lukas
  • 495
  • 4
  • 13

1 Answers1

2

You have not specified image name anywhere in above code its hard to debug

Another way of doing this is

String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");    
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
// Write your image name here
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete (); 
try {
       FileOutputStream out = new FileOutputStream(file);
       finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
       out.flush();
       out.close();

} catch (Exception e) {
       e.printStackTrace();
}

also make sure you have below permissions in your manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

To immediately add image to gallery (Credit goes to Lukas)

 exploreActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); 
Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • Thank you, but this code brings me back to my old problem: http://stackoverflow.com/questions/21759476/android-save-bitmap-to-gallery-time-created-wrong I want that the image is exactly in the folder like the photos from normal camera app are and the date created have to be the correct – lukas Mar 11 '14 at 13:25
  • As far as i know the default folder for camera captures is DCIM. You can specify the folder name also. – Jitender Dev Mar 11 '14 at 13:29
  • The answer was solved, but i didnt realised that the name now is wrong – lukas Mar 11 '14 at 13:29
  • Your answer is correct, but the image dont appear immediately in the gallery. if you add this: exploreActivity.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory()))); (which add the image immediately to the gallery) to your answer, i will mark it as correct. – lukas Mar 11 '14 at 14:54