Example usage for android.os Environment DIRECTORY_PICTURES

List of usage examples for android.os Environment DIRECTORY_PICTURES

Introduction

In this page you can find the example usage for android.os Environment DIRECTORY_PICTURES.

Prototype

String DIRECTORY_PICTURES

To view the source code for android.os Environment DIRECTORY_PICTURES.

Click Source Link

Document

Standard directory in which to place pictures that are available to the user.

Usage

From source file:Main.java

/**
 * Construct a File for an xxxx.jpg image in the external storage directory
 * //from   w w w  .  j  a v  a2  s .c om
 * @param name
 *            the 'xxxx'
 * @return File
 */
public static File constructExternalImageFile(String name) {
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File imageFile = new File(storageDir, name + JPEG_EXTENSION);
    imageFile = imageFile.getAbsoluteFile();
    return imageFile;
}

From source file:Main.java

public static File buildImageFilePath(String filename) throws IOException {

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = filename + "_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );//ww  w  .j av  a 2 s . co m

    return image;
}

From source file:Main.java

public static File getOutputMediaFile(Context context, String mediaName) {
    File mediaStorageDir = null;/*  ww w.  ja v a2 s.  co  m*/
    try {
        //            mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), ImgFolderName);
        mediaStorageDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), ImgFolderName);
    } catch (Exception e) {
        e.printStackTrace();
    }

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;
        }
    }

    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + mediaName);
    return mediaFile;
}

From source file:Main.java

public static File getOutputMediaFile(int type, String folderName) {
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), folderName);
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(folderName, "failed to create directory");
            return null;
        }/*from ww  w  .j av a 2 s .  co m*/
    }
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "VID_" + timeStamp + ".mp4");
    } else if (type == MEDIA_TYPE_AUDIO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator + "AUD_" + timeStamp + ".3gp");
    } else {
        return null;
    }
    return mediaFile;
}

From source file:Main.java

public static File getPhotoDir() {
    File picDirectory = null;// ww w  .  j  av a 2s.  c o  m
    File pictureDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    picDirectory = new File(pictureDir, "Jing");
    if (!picDirectory.exists()) {
        if (!picDirectory.mkdirs()) {
            picDirectory = null;
        }
    }
    return picDirectory;
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );/*from   w w  w  .ja  va2 s.  c  o  m*/
}

From source file:Main.java

/** Create a File for saving an image or video */
public static File getOutputMediaFile() {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), APPDIR);
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(APPDIR, "failed to create directory");
            return null;
        }// ww  w . j  a va 2 s.c o m
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
    return mediaFile;
}

From source file:Main.java

public static File getOutputMediaFile() {

    // External sdcard location

    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);//  w  w w  . j ava 2s  . c o  m
    // 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;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
    File mediaFile;

    mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

    return mediaFile;
}

From source file:Main.java

public static Bitmap saveBitmapToFile(Activity activity, Bitmap bitmap) {
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
    File imageFile = new File(mPath);
    boolean create = imageFile.mkdirs();
    boolean canWrite = imageFile.canWrite();
    Calendar cal = Calendar.getInstance();
    String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE);

    String filename = null;//  w ww.  j av a  2s.  c  o m
    int i = 0;
    while (imageFile.exists()) {
        i++;
        filename = date + "_mandelbrot" + i + ".png";
        imageFile = new File(mPath, filename);
        boolean canWrite2 = imageFile.canWrite();

    }

    try {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        /*resultB*/bitmap.compress(CompressFormat.PNG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        //write the bytes in file
        FileOutputStream fos = new FileOutputStream(imageFile);
        fos.write(bitmapdata);
        fos.flush();
        fos.close();

        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(imageFile));
        activity.sendBroadcast(intent);

        displaySuccesToast(activity);
    } catch (FileNotFoundException e) {
        displayFileError(activity);
        e.printStackTrace();
    } catch (IOException e) {
        displayFileError(activity);
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.java

@SuppressLint("SimpleDateFormat")
public static File createImageFile() throws IOException {
    // Create an image file name
    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, /* prefix */
            JPEG_EXTENSION, /* suffix */
            storageDir /* directory */
    );//  w  w  w. ja va  2 s .co m
    return image;
}