Creates a file to store the picture that was taken - Android android.graphics

Android examples for android.graphics:Image Load Save

Description

Creates a file to store the picture that was taken

Demo Code

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

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

public class Main {

  /**//from   w  w  w  .jav a  2s  .  c  o m
   * Creates a file to store the picture that was taken.
   * 
   * @return The file path.
   * @throws java.io.IOException
   *           If the file cannot be created.
   */
  public static File createImageFile(Bitmap bm) throws IOException {
    // Create the image file.
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";

    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, ".jpg", storageDir);
    OutputStream out = new FileOutputStream(image);
    bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
    return image;
  }

}

Related Tutorials