save Bitmap to JPEG based on its size - Android android.graphics

Android examples for android.graphics:JPEG

Description

save Bitmap to JPEG based on its size

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 boolean saveProgressFromToGallery(Bitmap bitmap, String fileName) {

    File sdCardDirectory = Environment.getExternalStorageDirectory();
    String sdCardDirectoryPath = sdCardDirectory.getPath() + "FOLDER_NAME";
    File image = new File(sdCardDirectoryPath, fileName);
    boolean success = false;

    FileOutputStream outStream;//w  w w . jav a2  s  . co m
    try {
      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 true;
    } else {
      return false;
    }
  }

}

Related Tutorials