returning image File name - Android Graphics

Android examples for Graphics:Image File

Description

returning image File name

Demo Code


//package com.java2s;

import android.os.Environment;
import android.util.Log;
import java.io.File;

public class Main {
    private static final String IMAGE_DIRECTORY_NAME = "FloderName";
    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final String IMG_PREFIX = "IMG_";
    private static final String IMG_POSTFIX = ".png";

    /**/*w w w  .j a v a 2s.  co m*/
     * 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;
        }

    }

    public static boolean isExternalStorageWritable() {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            return true;
        }
        return false;
    }

    public static void deleteRecursive(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory()) {
            for (File child : fileOrDirectory.listFiles()) {
                deleteRecursive(child);
            }
        }

        fileOrDirectory.delete();
    }
}

Related Tutorials