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

@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public static long getSDFreeSize() {
    File path = Environment.getExternalStorageDirectory();
    if (path != null && path.exists() && path.isDirectory()) {
        StatFs sf = new StatFs(path.getPath());
        long blockSize = sf.getBlockSizeLong();
        long freeBlocks = sf.getAvailableBlocksLong();
        return (freeBlocks * blockSize) / 1024 / 1024;
    }//from  w  w w. ja va2s . c o m
    return -1;
}

From source file:Main.java

public static String getPhotoFileName() {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault());
    Date date = new Date();
    return Environment.getExternalStorageDirectory().getPath() + "/DCIM/100ANDRO/Todo_"
            + dateFormat.format(date) + ".jpg";
}

From source file:Main.java

public static String getAvailableExternalMemorySize(Context context) {
    if (externalMemoryAvailable()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return Formatter.formatFileSize(context, availableBlocks * blockSize);
    } else {//w  ww  . ja  v a 2  s.c o m
        return "ERROR";
    }
}

From source file:Main.java

/**
 * get all the log files to upload/*from  ww  w. j  a v  a  2 s  . c o  m*/
 * 
 * @return
 */
public static File[] getFilesToUpload() {
    File[] files = null;
    File directory = new File(Environment.getExternalStorageDirectory() + File.separator + folderName);
    if (directory.exists()) {
        files = directory.listFiles();
    }
    return files;
}

From source file:Main.java

public static void readFromLog(int lines, TextView t) {

    File sdcard = new File(Environment.getExternalStorageDirectory() + "/clr_log.file");

    //Read text from file
    StringBuilder text = new StringBuilder();

    int n = 0;/*from   ww  w .j  av  a2s .co  m*/

    try {
        BufferedReader br = new BufferedReader(new FileReader(sdcard));
        String line;

        while ((line = br.readLine()) != null && n <= lines) {
            text.append(line);
            text.append('\n');
            n++;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    //Set the text
    t.setText(text);
}

From source file:Main.java

public static void saveBitmap(Bitmap bitmap, String fileName) {
    long time = System.currentTimeMillis();
    String filePath = Environment.getExternalStorageDirectory() + "/" + fileName;
    try {//from  w  ww  .ja  va  2s  .  com
        OutputStream stream = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
        stream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    Log.d("artbook", "save bitmap time: " + (System.currentTimeMillis() - time));
}

From source file:Main.java

private static String getResourcesPath(Context context, String url) {

    String dir = String.format("/Android/data/%s/files/", context.getPackageName());
    File sd = Environment.getExternalStorageDirectory();
    File filePath = new File(sd.getPath() + dir + "savedResources");
    filePath.mkdirs();/* ww  w . j a  v a  2 s .co m*/

    return filePath + url.substring(url.lastIndexOf("/"));
}

From source file:Main.java

public static File getExtWorkingDir(Context context) {
    if (!isExternalStorageWritable())
        return null;
    File workingDir = new File(Environment.getExternalStorageDirectory() + File.separator + "data");
    if (!workingDir.exists())
        workingDir.mkdir();/*from www .j  av a 2  s  . c o  m*/
    workingDir = new File(workingDir + File.separator + getPackageName(context));
    if (!workingDir.exists())
        workingDir.mkdir();
    return workingDir;
}

From source file:Main.java

public static String getSavePath() {
    String savePath = null;//  w ww .java 2 s  .  co  m
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        savePath = Environment.getExternalStorageDirectory().getAbsolutePath();
    } else {
        File[] files = new File("/mnt/").listFiles();
        if (files.length > 0) {
            String filePath = null;
            for (int p = 0; p < files.length; p++) {
                if (files[p].isDirectory()) {
                    if (files[p].getPath().indexOf("sdcard") >= 0) {
                        StatFs st = new StatFs(files[p].getPath());
                        int blocksize = st.getBlockSize();
                        int blockcount = st.getBlockCount();
                        if ((blocksize * blockcount) > 0) {
                            filePath = files[p].getPath();
                        }
                    }

                }
            }
            if (filePath != null) {
                savePath = filePath;
            } else {
                savePath = null;
            }
        }
    }
    return savePath;
}

From source file:Main.java

public static void saveImage(Bitmap bmp) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/abcxyz");
    myDir.mkdirs();/*from ww w  . j av a  2  s  .c  o m*/
    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);
    String name = "Image-" + n + ".jpg";
    File file = new File(myDir, name);
    if (file.exists())
        file.delete();

    try {
        FileOutputStream out = new FileOutputStream(file);
        bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}