30

I created a bitmap and now i want to save that bitmap to a directory somewhere. Can anyone show me how this is done. Thanks

FileInputStream in;
          BufferedInputStream buf;
           try {
                  in = new FileInputStream("/mnt/sdcard/dcim/Camera/2010-11-16_18-57-18_989.jpg");
                  buf = new BufferedInputStream(in);
                  Bitmap _bitmapPreScale = BitmapFactory.decodeStream(buf);
                  int oldWidth = _bitmapPreScale.getWidth();
                  int oldHeight = _bitmapPreScale.getHeight();
                  int newWidth = 2592; 
                  int newHeight = 1936;

                  float scaleWidth = ((float) newWidth) / oldWidth;
                  float scaleHeight = ((float) newHeight) / oldHeight;

                  Matrix matrix = new Matrix();
               // resize the bit map
                  matrix.postScale(scaleWidth, scaleHeight);
                  Bitmap _bitmapScaled = Bitmap.createBitmap(_bitmapPreScale, 0, 0,  oldWidth, oldHeight, matrix, true);

(I want to save _bitmapScaled to a folder on my SD card)

Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
user516883
  • 8,868
  • 23
  • 72
  • 114

6 Answers6

104

Hi You can write data to bytes and then create a file in sdcard folder with whatever name and extension you want and then write the bytes to that file. This will save bitmap to sdcard.

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.jpg");
f.createNewFile();
//write the bytes in file
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();
dakshbhatt21
  • 3,558
  • 3
  • 31
  • 40
krunal shah
  • 16,089
  • 25
  • 97
  • 143
12

You can also try this.

File file = new File(strDirectoy,imgname);
OutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
fOut.flush();
fOut.close();
MediaStore.Images.Media.insertImage(getContentResolver(),file.getAbsolutePath(),file.getName(),file.getName());
Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
4

just change the extension to .bmp.

Do this:

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
_bitmapScaled.compress(Bitmap.CompressFormat.PNG, 40, bytes);

//you can create a new file name "test.BMP" in sdcard folder.
File f = new File(Environment.getExternalStorageDirectory()
                        + File.separator + "test.bmp")

It'll sound that I'm just fooling around, but try it once and it'll get saved in BMP format. Cheers!

JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32
Mayank Saini
  • 3,017
  • 24
  • 25
4

_bitmapScaled.compress() should do the trick. Check out the docs: http://developer.android.com/reference/android/graphics/Bitmap.html#compress(android.graphics.Bitmap.CompressFormat, int, java.io.OutputStream)

EboMike
  • 76,846
  • 14
  • 164
  • 167
0

This answer is an update with a little more consideration for OOM and various other leaks.

Assumes you have a directory intended as the destination and a name String already defined.

    File destination = new File(directory.getPath() + File.separatorChar + filename);

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    source.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    FileOutputStream fo = null;
    try {
        destination.createNewFile();

        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
    } catch (IOException e) {

    } finally {
        try {
            fo.close();
        } catch (IOException e) {}
    }
Abandoned Cart
  • 4,512
  • 1
  • 34
  • 41
0

Pass bitmap to the saveImage Method, It will save your bitmap in the name of a saveBitmap, inside created test folder.

private void saveImage(Bitmap data) {
                    File createFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test");
                    if(!createFolder.exists())
                    createFolder.mkdir();
                    File saveImage = new File(createFolder,"saveBitmap.jpg");
                    try {
                        OutputStream outputStream = new FileOutputStream(saveImage);
                        data.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
                        outputStream.flush();
                        outputStream.close();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

and use this:

  saveImage(bitmap);
miladsolgi
  • 430
  • 5
  • 19
Shahzad Afridi
  • 2,058
  • 26
  • 24