Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.content.Context;

import android.os.Environment;
import java.io.File;
import java.io.IOException;

public class Main {
    /**
     * Create temp file. If has external storage create in external else create
     * in internal.
     *
     * @param context
     * @return
     * @throws IOException
     */
    public static File createTempFile(Context context) throws IOException {
        if (!hasExternalStorage()) {
            return createTempFile(context, context.getCacheDir());
        } else {
            return createTempFile(context, context.getExternalFilesDir("caches"));
        }
    }

    /**
     * Create temp file in folder
     *
     * @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);
    }

    /**
     * Check external exist or not.
     *
     * @return
     */
    public static boolean hasExternalStorage() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
}