save Bitmap To SDCARD - Android android.graphics

Android examples for android.graphics:Bitmap Load Save

Description

save Bitmap To SDCARD

Demo Code

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.os.Environment;

public class Main {

  public static String saveProgressimageToSDCARD(Bitmap bitmap, String fileName, String folderName) {
    File sdCardDirectory = new File(Environment.getExternalStorageDirectory().toString() + folderName);
    if (!sdCardDirectory.exists()) {
      sdCardDirectory.mkdirs();/*w ww.jav a  2  s  . c om*/
    }
    String sdCardDirectoryPath = sdCardDirectory.getPath();
    File image = new File(sdCardDirectoryPath, fileName);
    if (image.exists()) {
      image.delete();
    }

    boolean success = false;

    // Encode the file as a PNG image.
    // FileOutputStream outStream;
    try {
      // bitmap = rotateImage(bitmap, 90);
      FileOutputStream outStream = new FileOutputStream(image);
      if (bitmap.getHeight() < 2400) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
      } else if (bitmap.getHeight() > 2400 && bitmap.getHeight() < 3500) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
      } else if (bitmap.getHeight() > 3500) {
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
      }

      outStream.flush();
      outStream.close();
      success = true;
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    if (success) {
      return sdCardDirectoryPath + fileName;
    } else {
      return "";
    }
  }

}

Related Tutorials