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

Android examples for Media:Picture

Description

Creates a file to store the picture that was taken.

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.os.Environment;

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

public class Main {
    /**/*from  w  w w  . ja v a 2s.c  om*/
     * 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