Example usage for java.io File createTempFile

List of usage examples for java.io File createTempFile

Introduction

In this page you can find the example usage for java.io File createTempFile.

Prototype

public static File createTempFile(String prefix, String suffix, File directory) throws IOException 

Source Link

Document

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

Usage

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 */
    );//from w  ww  .j a v  a2 s.  c o  m
    return image;
}

From source file:Main.java

/**
 * Creates temporary file in external storage, or in case when extrnal storage is not available
 * temporary file is created in the internal storage.
 * @param context - Context/* w ww  .ja va 2  s  . c  o  m*/
 * @return instance of java.io.File or null if error occured.
 */
public static File createTempFile(Context context) {
    if (context == null)
        throw new IllegalArgumentException();

    File saveToDir = context.getExternalCacheDir();

    if (saveToDir == null) {
        saveToDir = context.getCacheDir();
    }

    File tempFile = null;

    try {
        tempFile = File.createTempFile("parrot", "", saveToDir);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

    return tempFile;
}

From source file:Main.java

/**
 * Creates a temporary file with the specified prefix and extension.
 * /*  w w  w .  j  ava2  s  . co m*/
 * @param part
 *            the prefix for the file
 * @param ext
 *            the extension for the file
 * @param context
 *            the application's context
 * @return the created File
 * @throws Exception
 */
private static File createTemporaryFile(String part, String ext, Context context) throws Exception {
    File tempDir = Environment.getExternalStorageDirectory();
    tempDir = new File(tempDir.getAbsolutePath() + "/bv-temp/");
    if (!tempDir.exists()) {
        tempDir.mkdir();
    }
    return File.createTempFile(part, ext, tempDir);
}

From source file:Main.java

public static Uri createImageFile() {
    // 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;//from w  w  w  . ja va2 s .c  om
    try {
        image = File.createTempFile(imageFileName, /* prefix */
                ".jpg", /* suffix */
                storageDir /* directory */
        );
    } catch (IOException e) {
        Log.d(TAG, "create tmp file error!", e);
        return null;
    }
    return Uri.fromFile(image);
}

From source file:byps.http.HTempFile.java

public static HTempFile createTemp(File tempDir, long streamId) throws IOException {
    HTempFile t = new HTempFile();
    if (streamId != 0) {
        String fileName = "bupload_" + streamId + ".tmp";
        t.file = new File(tempDir, fileName);
    } else {// www.  j av a  2s . c om
        t.file = File.createTempFile("bupload_", ".tmp", tempDir);
    }
    return t;
}

From source file:Main.java

public static boolean dirWritable(File dirFile) {
    try {/*  w ww  . ja va 2s.com*/
        if (!dirFile.isDirectory())
            return false;
        File testFile = File.createTempFile("test", "tmp", dirFile);
        testFile.delete();
        return true;
    } catch (IOException e) {
        return false;
    }
}

From source file:Main.java

/**
 * Creates a thumbnail file starting from an image/video file
 *
 * @param original_file original file path
 * @param video         true if the file is a video. false if the file is a picture
 * @return a file/*from w ww  .  j  a  v a2s .  com*/
 */
private static File generateThumbnailFileForFile(File original_file, boolean video) {
    // Create an image file name
    try {
        String imageFileName = "thumb" + original_file.getName();
        Log.d(TAG, original_file.getName());
        File storageDir = Environment.getExternalStoragePublicDirectory(
                video ? Environment.DIRECTORY_MOVIES : Environment.DIRECTORY_PICTURES);
        return File.createTempFile(imageFileName, /* prefix */
                THUMB_FORMAT_EXTENSION, /* suffix */
                storageDir /* directory */
        );
    } catch (IOException e) {
        Log.d(TAG, "Error", e);
    }
    return null;

}

From source file:Main.java

/**
 * Create temp file in folder//  www  .  j  a v  a2 s. c  o m
 *
 * @param context
 * @param folder  where place temp file
 * @return
 * @throws IOException
 */
public static File createTempFile(Context context, File folder) throws IOException {
    String prefix = String.valueOf(System.currentTimeMillis());
    return File.createTempFile(prefix, null, folder);
}

From source file:Main.java

public static File downSample(Context context, Uri uri) throws Exception {
    Bitmap b = null;/*w  w  w  .  ja v a  2s . c o  m*/

    //Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;

    int scale = 1;
    if (o.outHeight > MAX_SIZE || o.outWidth > MAX_SIZE) {
        scale = (int) Math.pow(2, (int) Math
                .round(Math.log(MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
    }

    //Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    InputStream is = context.getContentResolver().openInputStream(uri);
    b = BitmapFactory.decodeStream(is, null, o2);
    is.close();

    File outputDir = context.getCacheDir();
    File outputFile = File.createTempFile("avatar", ".jpg", outputDir);
    FileOutputStream fos = new FileOutputStream(outputFile);
    b.compress(Bitmap.CompressFormat.JPEG, 80, fos);
    fos.close();

    return outputFile;
}

From source file:Main.java

public static File createTmpFile(Context context) throws IOException {
    File dir = null;/* w  w w  .  j  av  a 2 s.  c  om*/
    if (TextUtils.equals(Environment.getExternalStorageState(), Environment.MEDIA_MOUNTED)) {
        dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        if (!dir.exists()) {
            dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Camera");
            if (!dir.exists()) {
                dir = getCacheDirectory(context, true);
            }
        }
    } else {
        dir = getCacheDirectory(context, true);
    }
    return File.createTempFile(JPEG_FILE_PREFIX, JPEG_FILE_SUFFIX, dir);
}