Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save Bitmap into File and return File having bitmap image

Tags:

android

I have a problem to save Bitmaps into files. My method is like this:

private File savebitmap(Bitmap bmp) {
    String extStorageDirectory = Environment.getExternalStorageDirectory()
            .toString();
    OutputStream outStream = null;

    File file = new File(bmp + ".png");
    if (file.exists()) {
        file.delete();
        file = new File(extStorageDirectory, bmp + ".png");
        Log.e("file exist", "" + file + ",Bitmap= " + bmp);
    }
    try {
        outStream = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

It gives me error of file.I am calling this method like this:

Drawable d = iv.getDrawable();
Bitmap bitmap = ((BitmapDrawable) d).getBitmap();
File file = savebitmap(bitmap);

Please help me...

like image 321
AndiM Avatar asked Mar 15 '13 09:03

AndiM


People also ask

How do I save a bitmap image in Java?

insertImage(getContentResolver(), bm, barcodeNumber + ". jpg Card Image", barcodeNumber + ". jpg Card Image"); Which works fine to save to SD card, but does not allow you to customize the folder.

How do I save a bitmap in SQL?

If you have Bitmap image then you can do following. Bitmap photo = <Your image> ByteArrayOutputStream bos = new ByteArrayOutputStream(); photo. compress(Bitmap. CompressFormat.

What is a recycled bitmap?

The recycle() method allows an app to reclaim memory as soon as possible. Caution: You should use recycle() only when you are sure that the bitmap is no longer being used. If you call recycle() and later attempt to draw the bitmap, you will get the error: "Canvas: trying to use a recycled bitmap" .


1 Answers

I try to make some corrections on your code I assume that you want to use filename instead of bitmap as parameter

 private File savebitmap(String filename) {
      String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
      OutputStream outStream = null;

      File file = new File(filename + ".png");
      if (file.exists()) {
         file.delete();
         file = new File(extStorageDirectory, filename + ".png");
         Log.e("file exist", "" + file + ",Bitmap= " + filename);
      }
      try {
         // make a new bitmap from your file
         Bitmap bitmap = BitmapFactory.decodeFile(file.getName());

         outStream = new FileOutputStream(file);
         bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
         outStream.flush();
         outStream.close();
      } catch (Exception e) {
         e.printStackTrace();
      }
      Log.e("file", "" + file);
      return file;

   }
like image 126
Festus Tamakloe Avatar answered Sep 19 '22 16:09

Festus Tamakloe