get Output Media File Uri to store image - Android android.graphics

Android examples for android.graphics:Image Load Save

Description

get Output Media File Uri to store image

Demo Code

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main{

    /**//from   w  w  w . j a  v a  2  s . c om
     * Creating file uri to store image
     */
    public static Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }
    /**
     * returning image
     */
    private static File getOutputMediaFile(int type) {

        // * Checks if external storage is available for read and write */
        if (isExternalStorageWritable()) {
            // External sdcard location
            File mediaStorageDir = new File(
                    Environment
                            .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    IMAGE_DIRECTORY_NAME);

            // Create the storage directory if it does not exist
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                            + IMAGE_DIRECTORY_NAME + " directory");
                    return null;
                }
            }

            File mediaFile;
            if (type == MEDIA_TYPE_IMAGE) {

                // if Directory exits  check if any file in there than
                // delete
                File[] contents = mediaStorageDir.listFiles();
                if (contents.length >= 1) {
                    deleteRecursive(mediaStorageDir);
                }
                mediaFile = new File(mediaStorageDir.getPath()
                        + File.separator + IMG_PREFIX + "temp"
                        + IMG_POSTFIX);
            } else {
                return null;
            }
            return mediaFile;

        } else {
            return null;
        }

    }

}

Related Tutorials