Skip to content

Instantly share code, notes, and snippets.

@Reacoder
Created August 8, 2014 10:38
Show Gist options
  • Save Reacoder/3797ab8b8e2e20714ea7 to your computer and use it in GitHub Desktop.
Save Reacoder/3797ab8b8e2e20714ea7 to your computer and use it in GitHub Desktop.
获得 sdcard 空间。
package com.ilegendsoft.toprankapps.tubeplayer.utils;
import android.os.Environment;
import android.os.StatFs;
public class FSUtil {
// One binary gigabyte equals 1,073,741,824 bytes.
private static final long PER_G_NUM = 1073741824;
public static String getAvailableSpace() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
// another method , can use
// Environment.getExternalStorageDirectory().getUsableSpace()
double sdAvailSize = (double) stat.getAvailableBlocks()
* (double) stat.getBlockSize();
if (sdAvailSize <= PER_G_NUM) {
double mAvailable = sdAvailSize * 1024 / PER_G_NUM;
return mAvailable + " M";
} else {
double gigaAvailable = sdAvailSize / PER_G_NUM;
return gigaAvailable + " G";
}
}
return null;
}
public static int getPercentage() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory()
.getPath());
long sdTotalSize = stat.getBlockCount();
long sdAvailSize = stat.getAvailableBlocks();
return (int) (sdAvailSize * 100 / sdTotalSize);
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment