Question

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.

Was it helpful?

Solution

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()))); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top