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

private static void initData(Activity activity) {
    mActivity = activity;/*  w w w. j av  a 2 s  . co m*/
    File mFile = new File(Environment.getExternalStorageDirectory() + "/jiayuan");
    if (!mFile.exists())
        mFile.mkdirs();
    mAvatar = new File(TEMP_IMG_PATH);
    if (mAvatar.exists())
        mAvatar.delete();
    mAvatarUri = Uri.fromFile(mAvatar);
}

From source file:Main.java

public static boolean hasEnoughSpace(float needSize) {
    if (isSDCardExist()) {
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());

        long blockSize;
        long availCount;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            blockSize = sf.getBlockSizeLong();
            availCount = sf.getAvailableBlocksLong();
        } else {//from w w  w  .  ja v  a 2 s .c o m
            blockSize = sf.getBlockSize();
            availCount = sf.getAvailableBlocks();
        }

        long restSize = availCount * blockSize;
        if (restSize > needSize) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

@SuppressWarnings("WeakerAccess")
public static String getPathForSharedStorage() {
    @SuppressWarnings("UnnecessaryLocalVariable")
    String storage = Environment.getExternalStorageDirectory().toString();
    //String storage = "/storage/emulated/0/"; // this is what my device is seeing for above call
    //String storage = "/sdcard"; // because gmail can't read it, as the file must be in a 'sharable' location
    return storage;
}

From source file:Main.java

public static String imageToLocal(Bitmap bitmap, String name) {
    String path = null;//from w w w  .  j a v a  2s  .  c o  m
    try {
        if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            return null;
        }
        String filePath = Environment.getExternalStorageDirectory().getPath() + "/cache/";
        File file = new File(filePath, name);
        if (file.exists()) {
            path = file.getAbsolutePath();
            return path;
        } else {
            file.getParentFile().mkdirs();
        }
        file.createNewFile();

        OutputStream outStream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        outStream.flush();
        outStream.close();
        if (!bitmap.isRecycled()) {
            bitmap.recycle();
        }

        path = file.getAbsolutePath();
    } catch (Exception e) {
    }
    return path;
}

From source file:Main.java

public static String getPhotoSavePath() {
    String dcimDir = null;//w  w  w . j  a v a  2 s .  co m

    File extDcimDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (extDcimDir == null || !extDcimDir.exists()) {
        File extDir = Environment.getExternalStorageDirectory();
        if (extDir == null) {
            return null;
        }
        dcimDir = extDir.getAbsolutePath() + "/DCIM";
        try {
            new File(dcimDir).mkdirs();
        } catch (Exception e) {
        }
    } else {
        dcimDir = extDcimDir.getAbsolutePath();
    }
    return dcimDir + PHOTO_FOLDER;
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static long getExternalStorageFreeMemory() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdAvailSize;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        sdAvailSize = (double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong();
    } else {/*  w w w  .  j  a v a  2  s.com*/
        sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
    }

    return Math.round(sdAvailSize / SIZE_MB);
}

From source file:Main.java

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static Boolean hasExternalStorageFreeMemory() {
    StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
    double sdAvailSize;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        sdAvailSize = (double) stat.getAvailableBlocksLong() * (double) stat.getBlockSizeLong();
    } else {/*from  www.j  av  a 2  s  .  c o m*/
        sdAvailSize = (double) stat.getAvailableBlocks() * (double) stat.getBlockSize();
    }

    return sdAvailSize >= 10 * SIZE_MB;
}

From source file:Main.java

/**
 * get the FileInputStream//from  w w  w .j a va2s  .co  m
 * @param filePath, it must contains the head "/"
 * @return
 * @throws FileNotFoundException if the file is not exist
 */
public static FileInputStream getFileInputStream(String filePath) throws FileNotFoundException {
    FileInputStream fins = null;
    File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + filePath);
    fins = new FileInputStream(file);
    return fins;
}

From source file:Main.java

public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
            || !Environment.isExternalStorageRemovable() ? Environment.getExternalStorageDirectory().getPath()
                    : context.getCacheDir().getPath();
    return new File(cachePath + File.separator + uniqueName);
}

From source file:Main.java

public static void writeLifeLogCountInFile(int walking, int running, int vehicle, int bicycle, String filename)
        throws IOException {
    Date date = new Date();
    BufferedWriter writer = null;
    File dir = new File(Environment.getExternalStorageDirectory() + "/LifeLog");
    Log.d("Directory PATH", dir.getAbsolutePath());
    boolean flag = dir.mkdir();
    Log.d("Directory created?", "" + flag);
    File file = new File(dir.getAbsolutePath(), filename);
    if (file.exists() == false) {
        file.createNewFile();/*from   w ww . j  a v a2  s  . c om*/
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write("Date,Walking,Running,Bicycle,Vehicle");
        writer.newLine();
        writer.write(date.toString() + "," + walking + "," + running + "," + bicycle + "," + vehicle);
        writer.newLine();
    } else {
        writer = new BufferedWriter(new FileWriter(file, true));
        writer.write(date.toString() + "," + walking + "," + running + "," + bicycle + "," + vehicle);
        writer.newLine();
        Log.d("Appended", "True");
    }
    writer.flush();
    writer.close();

}