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

private static File getTempFile(Context context) {
    File file = null;/*from  w  w  w  .  j  a  v  a2  s .c  o  m*/
    try {
        String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        file = File.createTempFile(fileName, ".jpg", context.getCacheDir());
    } catch (IOException e) {
    }
    return file;
}

From source file:Main.java

public static File newTempFile(Context context, String prefix, String suffix) throws IOException {
    return File.createTempFile(prefix, suffix, getDefaultCacheDir(context));
}

From source file:Main.java

public static File makeTmpFile(Context context, String prefix, String suffix) {
    File tmpOutFile = null;//from ww  w  .  jav a 2s  .  com
    try {
        //            File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File dir = ContextCompat.getExternalCacheDirs(context)[0];
        tmpOutFile = File.createTempFile(prefix, suffix, dir);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return tmpOutFile;
}

From source file:Main.java

public static File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
    return File.createTempFile(imageFileName, JPG_EXTENSION, getAlbumDir());
}

From source file:Main.java

private static String storeImage(Context context, Bitmap bitmap) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bytes);

    File f = null;/*from w  w  w .j av  a2  s  .  c o  m*/
    try {
        f = File.createTempFile("citationsImg", ".png", context.getExternalCacheDir());
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return f.getAbsolutePath();
}

From source file:Main.java

public static File getNewImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    return File.createTempFile(imageFileName, ".jpg", storageDir);
}

From source file:Main.java

public static File storeToFile(Context context, InputStream is) throws IOException {
    if (is == null) {
        return null;
    }//from ww  w.  j  a  v  a2  s  .com
    OutputStream os = null;
    File f = null;
    try {
        File outputDir = getSDPath(context);
        f = File.createTempFile("qiniu-", "", outputDir);
        os = new FileOutputStream(f);
        byte[] buffer = new byte[64 * 1024];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
    } catch (IOException e) {
        if (f != null) {
            f.delete();
            f = null;
        }
        throw e;
    } finally {
        try {
            is.close();
        } catch (Exception e) {
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
            }
        }
    }

    return f;
}

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 . ja va2  s .  c  o  m

    return image;
}

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  ww.j av a 2 s  . c o m*/
}

From source file:Main.java

/**
 * Get Image Uri when clicked from Camera
 *
 * @return Uri of clicked Image/*w w  w.  j  a  v a  2 s.  c o m*/
 */
public static Uri getBitmapUri() {
    File imageStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "CRMnextImages");
    if (!imageStorageDir.exists()) {
        imageStorageDir.mkdirs();
    }
    File file = null;
    try {
        file = File.createTempFile("IMG_" + String.valueOf(System.currentTimeMillis()), ".jpg",
                imageStorageDir);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return Uri.fromFile(file);
}