Skip to content

Instantly share code, notes, and snippets.

@sbelloz
Created November 2, 2015 02:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sbelloz/053425bb74db34bcbf22 to your computer and use it in GitHub Desktop.
Save sbelloz/053425bb74db34bcbf22 to your computer and use it in GitHub Desktop.
Util class for check available free space in internal/external memory
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import java.io.File;
public class MemoryUtils {
public final static int MIN_SIZE_MB = 100;
public final static int MAX_SIZE_MB = 300;
static final int ERROR = -1;
public static boolean externalMemoryAvailable() {
return android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
}
/**
* get available memory in MB in external storage if exists
*
* @return free space in MB, if external storage not exists, returns -1
*/
public static long getAvailableExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = getBlockSize(stat);
long availableBlocks = getBlockCount(stat);
return (availableBlocks * blockSize)
/ (1024 * 1024);
} else {
return ERROR;
}
}
static public long getTotalExternalMemorySize() {
if (externalMemoryAvailable()) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = getBlockSize(stat);
long totalBlocks = getBlockCount(stat);
return totalBlocks * blockSize;
} else {
return ERROR;
}
}
public static StatFs getInternalStatFs() {
File path = Environment.getDataDirectory();
return new StatFs(path.getPath());
}
public static StatFs getExternalStatFs() {
File path = Environment.getExternalStorageDirectory();
return new StatFs(path.getPath());
}
public static long getAvailableInternalMemorySize(StatFs stat) {
long blockSize = getBlockSize(stat);
long availableBlocks = getBlockCount(stat);
return availableBlocks * blockSize;
}
public static long getTotalInternalMemorySize(StatFs stat) {
long blockSize = getBlockSize(stat);
long totalBlocks = getBlockCount(stat);
return totalBlocks * blockSize;
}
private static long getBlockSize(StatFs stat) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return stat.getBlockSizeLong();
} else {
return stat.getBlockSize();
}
}
private static long getBlockCount(StatFs stat) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
return stat.getBlockCountLong();
} else {
return stat.getBlockCount();
}
}
public static String formatSize(long size) {
String suffix = null;
if (size >= 1024) {
suffix = "KB";
size /= 1024;
if (size >= 1024) {
suffix = "MB";
size /= 1024;
}
}
StringBuilder resultBuffer = new StringBuilder(Long.toString(size));
int commaOffset = resultBuffer.length() - 3;
while (commaOffset > 0) {
resultBuffer.insert(commaOffset, ',');
commaOffset -= 3;
}
if (suffix != null) resultBuffer.append(suffix);
return resultBuffer.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment