如何查看安装在 Android 设备上的 SD 卡有多少可用空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check how much free space an SD card mounted on an Android device has?
提问by Andy
How can I programmatically check how much free space an SD card mounted on an Android device has?
如何以编程方式检查安装在 Android 设备上的 SD 卡有多少可用空间?
回答by Rick
To get the external SD card's available "free" space to show a number which agrees with the Menu->Settings->SD card and phone storage's number, use the following code:
要获得外部 SD 卡的可用“空闲”空间以显示与菜单->设置->SD 卡和手机存储号码一致的数字,请使用以下代码:
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
double sdAvailSize = (double)stat.getAvailableBlocks()
* (double)stat.getBlockSize();
//One binary gigabyte equals 1,073,741,824 bytes.
double gigaAvailable = sdAvailSize / 1073741824;
Relevant documentation: http://developer.android.com/reference/android/os/StatFs.html
相关文档:http: //developer.android.com/reference/android/os/StatFs.html
回答by Muhammad Nabeel Arif
/**
* This class is designed to get available space in external storage of android.
* It contains methods which provide you the available space in different units e.g
* bytes, KB, MB, GB. OR you can get the number of available blocks on external storage.
*
*/
public class AvailableSpaceHandler {
//*********
//Variables
/**
* Number of bytes in one KB = 2<sup>10</sup>
*/
public final static long SIZE_KB = 1024L;
/**
* Number of bytes in one MB = 2<sup>20</sup>
*/
public final static long SIZE_MB = SIZE_KB * SIZE_KB;
/**
* Number of bytes in one GB = 2<sup>30</sup>
*/
public final static long SIZE_GB = SIZE_KB * SIZE_KB * SIZE_KB;
//********
// Methods
/**
* @return Number of bytes available on external storage
*/
public static long getExternalAvailableSpaceInBytes() {
long availableSpace = -1L;
try {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
} catch (Exception e) {
e.printStackTrace();
}
return availableSpace;
}
/**
* @return Number of kilo bytes available on external storage
*/
public static long getExternalAvailableSpaceInKB(){
return getExternalAvailableSpaceInBytes()/SIZE_KB;
}
/**
* @return Number of Mega bytes available on external storage
*/
public static long getExternalAvailableSpaceInMB(){
return getExternalAvailableSpaceInBytes()/SIZE_MB;
}
/**
* @return gega bytes of bytes available on external storage
*/
public static long getExternalAvailableSpaceInGB(){
return getExternalAvailableSpaceInBytes()/SIZE_GB;
}
/**
* @return Total number of available blocks on external storage
*/
public static long getExternalStorageAvailableBlocks() {
long availableBlocks = -1L;
try {
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
availableBlocks = stat.getAvailableBlocks();
} catch (Exception e) {
e.printStackTrace();
}
return availableBlocks;
}
}
回答by tknell
Here's a simpler way, that is usable since API level 9:
这是一种更简单的方法,自 API 级别 9 起可用:
Environment.getExternalStorageDirectory().getUsableSpace();
http://developer.android.com/reference/java/io/File.html#getUsableSpace()
http://developer.android.com/reference/java/io/File.html#getUsableSpace()
回答by Nuno Veloso
This code is working for me:
这段代码对我有用:
StatFs stat = new StatFs(System.getenv("SECONDARY_STORAGE"));
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
long kiloAvailable = bytesAvailable / 1024; // Available space from SD in KB
long megaAvailable = bytesAvailable / (1024*1024); // Available space from SD in MB
return (int)kiloAvailable;
回答by Javid Javidi
public static float megabytesAvailable(String StrPath) {
StatFs stat = new StatFs(StrPath);
long bytesAvailable = (long)stat.getBlockSize() * (long)stat.getAvailableBlocks();
return bytesAvailable / (1024.f * 1024.f);
}