save Photo To SDCard - Android android.hardware

Android examples for android.hardware:Photo

Description

save Photo 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.graphics.Bitmap.CompressFormat;

public class Main {

  public static void savePhotoToSDCard(Bitmap photoBitmap, String photoName, String path) {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {

      File dir = new File(path);
      if (!dir.exists()) {
        dir.mkdirs();/*from  w  w  w .  ja  va 2 s . c  o m*/
      }

      File photoFile = new File(path, photoName + ".jpg");
      try {

        FileOutputStream fileOutputStream = new FileOutputStream(photoFile);

        if (photoBitmap != null) {
          if (photoBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream)) {
            fileOutputStream.flush();
            fileOutputStream.close();
          }
        }

      } catch (FileNotFoundException e) {
        photoFile.delete();
        e.printStackTrace();
      } catch (IOException e) {
        photoFile.delete();
        e.printStackTrace();
      }

    }
  }

}

Related Tutorials