I am trying to figure out the available disk space on the Android phone running my application. Is there a way to do this programmatically?
我试图找出运行我的应用程序的Android手机上的可用磁盘空间。有没有办法以编程方式执行此操作?
Thanks,
9 个解决方案
#1
12
Try StatFs.getAvailableBlocks. You'll need to convert the block count to KB with getBlockSize.
试试StatFs.getAvailableBlocks。您需要使用getBlockSize将块计数转换为KB。
#2
45
Example: Getting human readable size like 1 Gb
示例:获得人类可读的大小,如1 Gb
String memory = bytesToHuman(totalMemory())
字符串内存= bytesToHuman(totalMemory())
/*************************************************************************************************
Returns size in bytes.
If you need calculate external memory, change this:
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this:
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
**************************************************************************************************/
public long totalMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long total = (statFs.getBlockCount() * statFs.getBlockSize());
return total;
}
public long freeMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
return free;
}
public long busyMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long total = (statFs.getBlockCount() * statFs.getBlockSize());
long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
long busy = total - free;
return busy;
}
Converting bytes to human readable format (like 1 Mb, 1 Gb)
将字节转换为人类可读格式(如1 Mb,1 Gb)
public static String floatForm (double d)
{
return new DecimalFormat("#.##").format(d);
}
public static String bytesToHuman (long size)
{
long Kb = 1 * 1024;
long Mb = Kb * 1024;
long Gb = Mb * 1024;
long Tb = Gb * 1024;
long Pb = Tb * 1024;
long Eb = Pb * 1024;
if (size < Kb) return floatForm( size ) + " byte";
if (size >= Kb && size < Mb) return floatForm((double)size / Kb) + " Kb";
if (size >= Mb && size < Gb) return floatForm((double)size / Mb) + " Mb";
if (size >= Gb && size < Tb) return floatForm((double)size / Gb) + " Gb";
if (size >= Tb && size < Pb) return floatForm((double)size / Tb) + " Tb";
if (size >= Pb && size < Eb) return floatForm((double)size / Pb) + " Pb";
if (size >= Eb) return floatForm((double)size / Eb) + " Eb";
return "???";
}
#3
6
Based on @XXX's answer, I've created a gist code snippet that wraps StatFs for easy and simple usage. You can find it here as a GitHub gist.
基于@XX的答案,我创建了一个包含StatF的gist代码片段,以方便和简单地使用。你可以在这里找到它作为GitHub的要点。
#4
6
There are some subtleties regarding paths that none of the current answers address. You must use the right path based on what kind of stats you are interested in. Based on a deep dive into DeviceStorageMonitorService.java which generates the low disk space warnings in the notification area and the sticky broadcasts for ACTION_DEVICE_STORAGE_LOW, here are some of the paths that you can use:
关于路径的一些细微之处,目前的答案都没有解决。您必须根据您感兴趣的统计数据使用正确的路径。基于深入了解通知区域中生成低磁盘空间警告的DeviceStorageMonitorService.java和ACTION_DEVICE_STORAGE_LOW的粘性广播,以下是一些路径你可以使用:
-
To check free internal disk space use the data directory obtained via Environment.getDataDirectory(). This will get you the free space on the data partition. The data partition hosts all the internal storage for all apps on the device.
要检查可用的内部磁盘空间,请使用通过Environment.getDataDirectory()获取的数据目录。这将为您提供数据分区上的可用空间。数据分区托管设备上所有应用程序的所有内部存储。
-
To check free external (SDCARD) disk space use the external storage directory obtained via Environment.getExternalStorageDirectory(). This will get you the free space on the SDCARD.
要检查空闲外部(SDCARD)磁盘空间,请使用通过Environment.getExternalStorageDirectory()获取的外部存储目录。这将为您提供SDCARD上的可用空间。
-
To check for available memory on the system partition which contains OS files, use Environment.getRootDirectory(). Since your app has no access to the system partition, this stat is probably not very useful. DeviceStorageMonitorService uses for informational purposes and enters it into a log.
要检查包含OS文件的系统分区上的可用内存,请使用Environment.getRootDirectory()。由于您的应用无法访问系统分区,因此该统计信息可能不太有用。 DeviceStorageMonitorService用于提供信息,并将其输入日志。
-
To check for temporary files / cache memory, use Environment.getDownloadCacheDirectory(). DeviceStorageMonitorService attempts to clean some of the temporary files when memory gets low.
要检查临时文件/缓存内存,请使用Environment.getDownloadCacheDirectory()。当内存不足时,DeviceStorageMonitorService会尝试清除某些临时文件。
Some sample code for getting the internal (/data), external (/sdcard) and OS (/system) free memory:
获取内部(/ data),外部(/ sdcard)和OS(/系统)可用内存的示例代码:
// Get internal (data partition) free space
// This will match what's shown in System Settings > Storage for
// Internal Space, when you subtract Total - Used
public long getFreeInternalMemory()
{
return getFreeMemory(Environment.getDataDirectory());
}
// Get external (SDCARD) free space
public long getFreeExternalMemory()
{
return getFreeMemory(Environment.getExternalStorageDirectory());
}
// Get Android OS (system partition) free space
public long getFreeSystemMemory()
{
return getFreeMemory(Environment.getRootDirectory());
}
// Get free space for provided path
// Note that this will throw IllegalArgumentException for invalid paths
public long getFreeMemory(File path)
{
StatFs stats = new StatFs(path.getAbsolutePath());
return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
}
#5
4
Typecast your integer values to long before doing multiplication. Multiplication between two big integers could overflow and give a negative result.
在进行乘法之前,将整数值强制转换为long。两个大整数之间的乘法可能会溢出并产生负面结果。
public long sd_card_free(){
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
int availBlocks = stat.getAvailableBlocks();
int blockSize = stat.getBlockSize();
long free_memory = (long)availBlocks * (long)blockSize;
return free_memory;
}
#6
3
With a little google you might had found the StatFs
-class which is:
有点谷歌你可能找到了StatFs类,它是:
[...] a Wrapper for Unix statfs().
[...] Unix for statfs()的包装器。
Examples are provided
提供了示例
- here
- and also here.
而且在这里。
#7
2
File pathOS = Environment.getRootDirectory();//Os Storage
StatFs statOS = new StatFs(pathOS.getPath());
File pathInternal = Environment.getDataDirectory();// Internal Storage
StatFs statInternal = new StatFs(pathInternal.getPath());
File pathSdcard = Environment.getExternalStorageDirectory();//External(SD CARD) Storage
StatFs statSdcard = new StatFs(pathSdcard.getPath());
if((android.os.Build.VERSION.SDK_INT < 18)) {
// Get Android OS (system partition) free space API 18 & Below
int totalBlocksOS = statOS.getBlockCount();
int blockSizeOS = statOS.getBlockSize();
int availBlocksOS = statOS.getAvailableBlocks();
long total_OS_memory = (long) totalBlocksOS * (long) blockSizeOS;
long free_OS_memory = (long) availBlocksOS * (long) blockSizeOS;
long Used_OS_memory = total_OS_memory - free_OS_memory;
TotalOsMemory = total_OS_memory ;
FreeOsMemory = free_OS_memory;
UsedOsMemory = Used_OS_memory;
// Get internal (data partition) free space API 18 & Below
int totalBlocksInternal = statInternal.getBlockCount();
int blockSizeInternal = statOS.getBlockSize();
int availBlocksInternal = statInternal.getAvailableBlocks();
long total_Internal_memory = (long) totalBlocksInternal * (long) blockSizeInternal;
long free_Internal_memory = (long) availBlocksInternal * (long) blockSizeInternal;
long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
TotalInternalMemory = total_Internal_memory;
FreeInternalMemory = free_Internal_memory ;
UsedInternalMemory = Used_Intenal_memory ;
// Get external (SDCARD) free space for API 18 & Below
int totalBlocksSdcard = statSdcard.getBlockCount();
int blockSizeSdcard = statOS.getBlockSize();
int availBlocksSdcard = statSdcard.getAvailableBlocks();
long total_Sdcard_memory = (long) totalBlocksSdcard * (long) blockSizeSdcard;
long free_Sdcard_memory = (long) availBlocksSdcard * (long) blockSizeSdcard;
long Used_Sdcard_memory = total_Sdcard_memory - free_Sdcard_memory;
TotalSdcardMemory = total_Sdcard_memory;
FreeSdcardMemory = free_Sdcard_memory;
UsedSdcardMemory = Used_Sdcard_memory;
}
else {
// Get Android OS (system partition) free space for API 18 & Above
long total_OS_memory = (statOS. getBlockCountLong() * statOS.getBlockSizeLong());
long free_OS_memory = (statOS. getAvailableBlocksLong() * statOS.getBlockSizeLong());
long Used_OS_memory = total_OS_memory - free_OS_memory;
TotalOsMemory = total_OS_memory ;
FreeOsMemory = free_OS_memory;
UsedOsMemory = Used_OS_memory;
// Get internal (data partition) free space for API 18 & Above
long total_Internal_memory = (statInternal. getBlockCountLong() * statInternal.getBlockSizeLong());
long free_Internal_memory = (statInternal. getAvailableBlocksLong() * statInternal.getBlockSizeLong());
long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
TotalInternalMemory = total_Internal_memory;
FreeInternalMemory = free_Internal_memory ;
UsedInternalMemory = Used_Intenal_memory ;
// Get external (SDCARD) free space for API 18 & Above
long total_Sdcard_memory = (statSdcard. getBlockCountLong() * statSdcard.getBlockSizeLong());
long free_Sdcard_memory = (statSdcard. getAvailableBlocksLong() * statSdcard.getBlockSizeLong());
long Used_Sdcard_memory = tota*emphasized text*l_Sdcard_memory - free_Sdcard_memory;
TotalSdcardMemory = total_Sdcard_memory;
FreeSdcardMemory = free_Sdcard_memory;
UsedSdcardMemory = Used_Sdcard_memory;
}
}
#8
0
Since blocksize and getAvailableBlocks
自blocksize和getAvailableBlocks
are deprecated
this code can be use
这段代码可以使用
note based above answer by user802467
基于用户802467的回答
public long sd_card_free(){
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long availBlocks = stat.getAvailableBlocksLong();
long blockSize = stat.getBlockSizeLong();
long free_memory = availBlocks * blockSize;
return free_memory;
}
we can use getAvailableBlocksLong
and getBlockSizeLong
我们可以使用getAvailableBlocksLong和getBlockSizeLong
#9
0
Memory Locations:
File[] roots = context.getExternalFilesDirs(null);
String path = roots[0].getAbsolutePath(); // PhoneMemory
String path = roots[1].getAbsolutePath(); // SCCard (if available)
String path = roots[2].getAbsolutePath(); // USB (if available)
usage
long totalMemory = StatUtils.totalMemory(path);
long freeMemory = StatUtils.freeMemory(path);
final String totalSpace = StatUtils.humanize(totalMemory, true);
final String usableSpace = StatUtils.humanize(freeMemory, true);
You can use this
你可以用它
public final class StatUtils {
public static long totalMemory(String path) {
StatFs statFs = new StatFs(path);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
//noinspection deprecation
return (statFs.getBlockCount() * statFs.getBlockSize());
} else {
return (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
}
}
public static long freeMemory(String path) {
StatFs statFs = new StatFs(path);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
//noinspection deprecation
return (statFs.getAvailableBlocks() * statFs.getBlockSize());
} else {
return (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
}
}
public static long usedMemory(String path) {
long total = totalMemory(path);
long free = freeMemory(path);
return total - free;
}
public static String humanize(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format(Locale.ENGLISH, "%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
}
#1
12
Try StatFs.getAvailableBlocks. You'll need to convert the block count to KB with getBlockSize.
试试StatFs.getAvailableBlocks。您需要使用getBlockSize将块计数转换为KB。
#2
45
Example: Getting human readable size like 1 Gb
示例:获得人类可读的大小,如1 Gb
String memory = bytesToHuman(totalMemory())
字符串内存= bytesToHuman(totalMemory())
/*************************************************************************************************
Returns size in bytes.
If you need calculate external memory, change this:
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this:
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());
**************************************************************************************************/
public long totalMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long total = (statFs.getBlockCount() * statFs.getBlockSize());
return total;
}
public long freeMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
return free;
}
public long busyMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long total = (statFs.getBlockCount() * statFs.getBlockSize());
long free = (statFs.getAvailableBlocks() * statFs.getBlockSize());
long busy = total - free;
return busy;
}
Converting bytes to human readable format (like 1 Mb, 1 Gb)
将字节转换为人类可读格式(如1 Mb,1 Gb)
public static String floatForm (double d)
{
return new DecimalFormat("#.##").format(d);
}
public static String bytesToHuman (long size)
{
long Kb = 1 * 1024;
long Mb = Kb * 1024;
long Gb = Mb * 1024;
long Tb = Gb * 1024;
long Pb = Tb * 1024;
long Eb = Pb * 1024;
if (size < Kb) return floatForm( size ) + " byte";
if (size >= Kb && size < Mb) return floatForm((double)size / Kb) + " Kb";
if (size >= Mb && size < Gb) return floatForm((double)size / Mb) + " Mb";
if (size >= Gb && size < Tb) return floatForm((double)size / Gb) + " Gb";
if (size >= Tb && size < Pb) return floatForm((double)size / Tb) + " Tb";
if (size >= Pb && size < Eb) return floatForm((double)size / Pb) + " Pb";
if (size >= Eb) return floatForm((double)size / Eb) + " Eb";
return "???";
}
#3
6
Based on @XXX's answer, I've created a gist code snippet that wraps StatFs for easy and simple usage. You can find it here as a GitHub gist.
基于@XX的答案,我创建了一个包含StatF的gist代码片段,以方便和简单地使用。你可以在这里找到它作为GitHub的要点。
#4
6
There are some subtleties regarding paths that none of the current answers address. You must use the right path based on what kind of stats you are interested in. Based on a deep dive into DeviceStorageMonitorService.java which generates the low disk space warnings in the notification area and the sticky broadcasts for ACTION_DEVICE_STORAGE_LOW, here are some of the paths that you can use:
关于路径的一些细微之处,目前的答案都没有解决。您必须根据您感兴趣的统计数据使用正确的路径。基于深入了解通知区域中生成低磁盘空间警告的DeviceStorageMonitorService.java和ACTION_DEVICE_STORAGE_LOW的粘性广播,以下是一些路径你可以使用:
-
To check free internal disk space use the data directory obtained via Environment.getDataDirectory(). This will get you the free space on the data partition. The data partition hosts all the internal storage for all apps on the device.
要检查可用的内部磁盘空间,请使用通过Environment.getDataDirectory()获取的数据目录。这将为您提供数据分区上的可用空间。数据分区托管设备上所有应用程序的所有内部存储。
-
To check free external (SDCARD) disk space use the external storage directory obtained via Environment.getExternalStorageDirectory(). This will get you the free space on the SDCARD.
要检查空闲外部(SDCARD)磁盘空间,请使用通过Environment.getExternalStorageDirectory()获取的外部存储目录。这将为您提供SDCARD上的可用空间。
-
To check for available memory on the system partition which contains OS files, use Environment.getRootDirectory(). Since your app has no access to the system partition, this stat is probably not very useful. DeviceStorageMonitorService uses for informational purposes and enters it into a log.
要检查包含OS文件的系统分区上的可用内存,请使用Environment.getRootDirectory()。由于您的应用无法访问系统分区,因此该统计信息可能不太有用。 DeviceStorageMonitorService用于提供信息,并将其输入日志。
-
To check for temporary files / cache memory, use Environment.getDownloadCacheDirectory(). DeviceStorageMonitorService attempts to clean some of the temporary files when memory gets low.
要检查临时文件/缓存内存,请使用Environment.getDownloadCacheDirectory()。当内存不足时,DeviceStorageMonitorService会尝试清除某些临时文件。
Some sample code for getting the internal (/data), external (/sdcard) and OS (/system) free memory:
获取内部(/ data),外部(/ sdcard)和OS(/系统)可用内存的示例代码:
// Get internal (data partition) free space
// This will match what's shown in System Settings > Storage for
// Internal Space, when you subtract Total - Used
public long getFreeInternalMemory()
{
return getFreeMemory(Environment.getDataDirectory());
}
// Get external (SDCARD) free space
public long getFreeExternalMemory()
{
return getFreeMemory(Environment.getExternalStorageDirectory());
}
// Get Android OS (system partition) free space
public long getFreeSystemMemory()
{
return getFreeMemory(Environment.getRootDirectory());
}
// Get free space for provided path
// Note that this will throw IllegalArgumentException for invalid paths
public long getFreeMemory(File path)
{
StatFs stats = new StatFs(path.getAbsolutePath());
return stats.getAvailableBlocksLong() * stats.getBlockSizeLong();
}
#5
4
Typecast your integer values to long before doing multiplication. Multiplication between two big integers could overflow and give a negative result.
在进行乘法之前,将整数值强制转换为long。两个大整数之间的乘法可能会溢出并产生负面结果。
public long sd_card_free(){
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
int availBlocks = stat.getAvailableBlocks();
int blockSize = stat.getBlockSize();
long free_memory = (long)availBlocks * (long)blockSize;
return free_memory;
}
#6
3
With a little google you might had found the StatFs
-class which is:
有点谷歌你可能找到了StatFs类,它是:
[...] a Wrapper for Unix statfs().
[...] Unix for statfs()的包装器。
Examples are provided
提供了示例
- here
- and also here.
而且在这里。
#7
2
File pathOS = Environment.getRootDirectory();//Os Storage
StatFs statOS = new StatFs(pathOS.getPath());
File pathInternal = Environment.getDataDirectory();// Internal Storage
StatFs statInternal = new StatFs(pathInternal.getPath());
File pathSdcard = Environment.getExternalStorageDirectory();//External(SD CARD) Storage
StatFs statSdcard = new StatFs(pathSdcard.getPath());
if((android.os.Build.VERSION.SDK_INT < 18)) {
// Get Android OS (system partition) free space API 18 & Below
int totalBlocksOS = statOS.getBlockCount();
int blockSizeOS = statOS.getBlockSize();
int availBlocksOS = statOS.getAvailableBlocks();
long total_OS_memory = (long) totalBlocksOS * (long) blockSizeOS;
long free_OS_memory = (long) availBlocksOS * (long) blockSizeOS;
long Used_OS_memory = total_OS_memory - free_OS_memory;
TotalOsMemory = total_OS_memory ;
FreeOsMemory = free_OS_memory;
UsedOsMemory = Used_OS_memory;
// Get internal (data partition) free space API 18 & Below
int totalBlocksInternal = statInternal.getBlockCount();
int blockSizeInternal = statOS.getBlockSize();
int availBlocksInternal = statInternal.getAvailableBlocks();
long total_Internal_memory = (long) totalBlocksInternal * (long) blockSizeInternal;
long free_Internal_memory = (long) availBlocksInternal * (long) blockSizeInternal;
long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
TotalInternalMemory = total_Internal_memory;
FreeInternalMemory = free_Internal_memory ;
UsedInternalMemory = Used_Intenal_memory ;
// Get external (SDCARD) free space for API 18 & Below
int totalBlocksSdcard = statSdcard.getBlockCount();
int blockSizeSdcard = statOS.getBlockSize();
int availBlocksSdcard = statSdcard.getAvailableBlocks();
long total_Sdcard_memory = (long) totalBlocksSdcard * (long) blockSizeSdcard;
long free_Sdcard_memory = (long) availBlocksSdcard * (long) blockSizeSdcard;
long Used_Sdcard_memory = total_Sdcard_memory - free_Sdcard_memory;
TotalSdcardMemory = total_Sdcard_memory;
FreeSdcardMemory = free_Sdcard_memory;
UsedSdcardMemory = Used_Sdcard_memory;
}
else {
// Get Android OS (system partition) free space for API 18 & Above
long total_OS_memory = (statOS. getBlockCountLong() * statOS.getBlockSizeLong());
long free_OS_memory = (statOS. getAvailableBlocksLong() * statOS.getBlockSizeLong());
long Used_OS_memory = total_OS_memory - free_OS_memory;
TotalOsMemory = total_OS_memory ;
FreeOsMemory = free_OS_memory;
UsedOsMemory = Used_OS_memory;
// Get internal (data partition) free space for API 18 & Above
long total_Internal_memory = (statInternal. getBlockCountLong() * statInternal.getBlockSizeLong());
long free_Internal_memory = (statInternal. getAvailableBlocksLong() * statInternal.getBlockSizeLong());
long Used_Intenal_memory = total_Internal_memory - free_Internal_memory;
TotalInternalMemory = total_Internal_memory;
FreeInternalMemory = free_Internal_memory ;
UsedInternalMemory = Used_Intenal_memory ;
// Get external (SDCARD) free space for API 18 & Above
long total_Sdcard_memory = (statSdcard. getBlockCountLong() * statSdcard.getBlockSizeLong());
long free_Sdcard_memory = (statSdcard. getAvailableBlocksLong() * statSdcard.getBlockSizeLong());
long Used_Sdcard_memory = tota*emphasized text*l_Sdcard_memory - free_Sdcard_memory;
TotalSdcardMemory = total_Sdcard_memory;
FreeSdcardMemory = free_Sdcard_memory;
UsedSdcardMemory = Used_Sdcard_memory;
}
}
#8
0
Since blocksize and getAvailableBlocks
自blocksize和getAvailableBlocks
are deprecated
this code can be use
这段代码可以使用
note based above answer by user802467
基于用户802467的回答
public long sd_card_free(){
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long availBlocks = stat.getAvailableBlocksLong();
long blockSize = stat.getBlockSizeLong();
long free_memory = availBlocks * blockSize;
return free_memory;
}
we can use getAvailableBlocksLong
and getBlockSizeLong
我们可以使用getAvailableBlocksLong和getBlockSizeLong
#9
0
Memory Locations:
File[] roots = context.getExternalFilesDirs(null);
String path = roots[0].getAbsolutePath(); // PhoneMemory
String path = roots[1].getAbsolutePath(); // SCCard (if available)
String path = roots[2].getAbsolutePath(); // USB (if available)
usage
long totalMemory = StatUtils.totalMemory(path);
long freeMemory = StatUtils.freeMemory(path);
final String totalSpace = StatUtils.humanize(totalMemory, true);
final String usableSpace = StatUtils.humanize(freeMemory, true);
You can use this
你可以用它
public final class StatUtils {
public static long totalMemory(String path) {
StatFs statFs = new StatFs(path);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
//noinspection deprecation
return (statFs.getBlockCount() * statFs.getBlockSize());
} else {
return (statFs.getBlockCountLong() * statFs.getBlockSizeLong());
}
}
public static long freeMemory(String path) {
StatFs statFs = new StatFs(path);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
//noinspection deprecation
return (statFs.getAvailableBlocks() * statFs.getBlockSize());
} else {
return (statFs.getAvailableBlocksLong() * statFs.getBlockSizeLong());
}
}
public static long usedMemory(String path) {
long total = totalMemory(path);
long free = freeMemory(path);
return total - free;
}
public static String humanize(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1) + (si ? "" : "i");
return String.format(Locale.ENGLISH, "%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
}