Example usage for android.os Environment getExternalStorageDirectory

List of usage examples for android.os Environment getExternalStorageDirectory

Introduction

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

Prototype

public static File getExternalStorageDirectory() 

Source Link

Document

Return the primary shared/external storage directory.

Usage

From source file:Main.java

public static boolean checkSDCardAvailabel() {
    return Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED);
}

From source file:Main.java

public static String getAbsolutePathOnExternalStorage(final Context pContext, final String pFilePath) {
    return Environment.getExternalStorageDirectory() + "/Android/data/"
            + pContext.getApplicationInfo().packageName + "/files/" + pFilePath;
}

From source file:Main.java

public static long getTotalExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        mStatFs.restat(path.getPath());//  w  w  w .  ja  v a 2  s  .  co m
        long blockSize = mStatFs.getBlockSize();
        long totalBlocks = mStatFs.getBlockCount();
        return totalBlocks * blockSize;
    } else {
        return -1;
    }
}

From source file:Main.java

public static boolean hasSDCard(Context context) {
    if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
        String dir = Environment.getExternalStorageDirectory() + FILE_DIR;
        File f = new File(dir);
        if (!f.exists()) {
            f.mkdir();/*w w  w .  j  ava  2 s. c  o  m*/
        } else {
            try {
                for (File t : f.listFiles()) {
                    t.delete();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return true;
    }
    return false;
}

From source file:Main.java

public static long getAvailableExternalMemorySize() {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        mStatFs.restat(path.getPath());/*from   w w  w . j  a  va 2  s.c  o m*/
        long blockSize = mStatFs.getBlockSize();
        long availableBlocks = mStatFs.getAvailableBlocks();
        return availableBlocks * blockSize;
    } else {
        return -1;
    }
}

From source file:Main.java

public static File bitmap2File(Bitmap bitmap, String filename) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    OutputStream outStream = null;

    File file = new File(extStorageDirectory, filename + ".png");
    if (file.exists()) {
        file.delete();/*from   ww  w  .  j  a  va2 s. c  o m*/
        file = new File(extStorageDirectory, filename + ".png");
        Log.e("file exist", "" + file + ",Bitmap= " + filename);
    }
    try {
        outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Log.e("file", "" + file);
    return file;

}

From source file:Main.java

private static String createDir(String path) {
    boolean isHaveSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    if (!isHaveSDCard) {
        return null;
    }/*from  ww  w .ja va2  s.c  o m*/
    File directory = Environment.getExternalStorageDirectory();
    File file = new File(directory.getAbsolutePath() + path);
    if (!file.exists()) {
        boolean isSuccess = file.mkdirs();
        if (isSuccess) {
            return file.getPath() + File.separator;
        } else {
            return null;
        }
    }
    return file.getPath() + File.separator;
}

From source file:Main.java

/**
 * Saves a Bitmap object to disk for analysis.
 *
 * @param bitmap The bitmap to save./* w w  w .j  a va 2s  . c  o  m*/
 */
public static void saveBitmap(final Bitmap bitmap) {
    final String root = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
            + "tensorflow";
    //LOGGER.i("Saving %dx%d bitmap to %s.", bitmap.getWidth(), bitmap.getHeight(), root);
    final File myDir = new File(root);

    if (!myDir.mkdirs()) {
        //LOGGER.i("Make dir failed");
    }

    final String fname = "preview.png";
    final File file = new File(myDir, fname);
    if (file.exists()) {
        file.delete();
    }
    try {
        final FileOutputStream out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 99, out);
        out.flush();
        out.close();
    } catch (final Exception e) {
        //LOGGER.e(e, "Exception!");
    }
}

From source file:Main.java

@SuppressLint("SdCardPath")
public static String getSDPath() {
    File sdDir = null;/*from w w w.ja va 2  s  . c o  m*/
    boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if (sdCardExist) {
        sdDir = Environment.getExternalStorageDirectory();
    }
    return sdDir.toString();
}

From source file:Main.java

public static boolean importDatabase(String dbName) {

    try {//from w  w  w .jav  a2s  .c o  m
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + PACKAGENAME + "//databases//" + dbName;
            String backupDBPath = "/AttendanceManager/" + dbName;
            File backupDB = new File(data, currentDBPath);
            File currentDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
        }
    } catch (Exception e) {
        return false;
    }
    return true;
}