0

I am using ACTION_IMAGE_CAPTURE to capture image using camera. It works fine, But the problem is that image is showing in imageview after clicking but can not saved in external or internal storage. Here is my code for saving image in external storage.

Bitmap bitmap = (Bitmap) data.getExtras().get("data");
        File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DashBoard/");
        file.mkdirs();
        ticket = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DashBoard/Ticket.jpg";
        file4 = new File(file, ticket);
        try {
            FileOutputStream out = new FileOutputStream(file4);
            bitmap.compress(Bitmap.CompressFormat.JPEG , 90, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        ivTicket.setImageBitmap(bitmap);

Any solution ?

3 Answers3

1

Use ImageWorker Library for easily saving bitmaps/drawables/base64.

How to Save

ImageWorker.to(context).
    directory("ImageWorker").
    subDirectory("SubDirectory").
    setFileName("Image").
    withExtension(Extension.PNG).
    save(sourceBitmap,85)

Easy as that. Contributions are welcomed.

Himanshu Rawat
  • 658
  • 5
  • 13
0

Follow it -

private void SaveImage(Bitmap finalBitmap) {

    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);
    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();
    }
}

and add this in manifest

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

EDIT: By using this line you will be able to see saved images in the gallery view.

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                         Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Reference

AGM Tazim
  • 2,213
  • 3
  • 16
  • 25
-2

Try This:

private void SaveImage(Bitmap finalBitmap) {
 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);
 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();
}
}

and add this in manifest

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

another example here

UPDATE instead of Environment.getExternalStorageDirectory() you can use your storage location as API level 30 and above Storage-policy is changed.