我想在Android上获得存储空间。这是我找到的解决方案。
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath())
stat.getTotalBytes()
当设置应用显示774 MB
时,它会返回1 GB
。在AVD Manager中,磁盘上的大小为1 GB
。是否可以像设置应用程序一样获得相同的值?
答案 0 :(得分:1)
尝试,
StatFs stat1 = new StatFs("YOUR PATH HERE");
long bytesAvailable1 = (long) stat1.getAvailableBlocks() * (long) stat1.getBlockSize();
double spaceAvailableInSD1 = Double.parseDouble(new DecimalFormat("##.##").format(bytesAvailable1 / 1073741824d)); //return MB.
在我的情况下,我获得了确切的可用存储空间。
答案 1 :(得分:-1)
下面是检查是否有外部SD卡,内部存储空间和外部SD卡的总空间和可用空间的方法
public boolean isExternalSDCardAvailable() {
String secStore = System.getenv("SECONDARY_STORAGE");
if (secStore != null) {
File f_secs = new File(secStore);
return f_secs.exists();
} else return false;
}
public long getAvailableInternalMemorySize() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = stat.getBlockSizeLong() * stat.getFreeBlocksLong();
return bytesAvailable / (1024 * 1024);
} else {
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getFreeBlocks();
return bytesAvailable / (1024 * 1024);
}
}
public long getTotalInternalMemorySize() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = stat.getBlockSizeLong() * stat.getBlockCountLong();
return bytesAvailable / (1024 * 1024);
} else {
StatFs stat = new StatFs(Environment.getDataDirectory().getPath());
long bytesAvailable = (long) stat.getBlockSize() * (long) stat.getAvailableBlocks();
return bytesAvailable / (1024 * 1024);
}
}
public long getAvailableExternalMemorySize() {
String secStore = System.getenv("SECONDARY_STORAGE");
File f_secs = new File(secStore);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
StatFs stat = new StatFs(f_secs.getPath());
long bytesAvailable = stat.getBlockSizeLong() * stat.getAvailableBlocksLong();
return bytesAvailable / (1024 * 1024);
} else {
StatFs stat = new StatFs(f_secs.getPath());
long bytesAvailable = stat.getBlockSize() * stat.getAvailableBlocks();
return bytesAvailable / (1024 * 1024);
}
}
public Long getTotalExternalMemorySize() {
String secStore = System.getenv("SECONDARY_STORAGE");
File f_secs = new File(secStore);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
StatFs stat = new StatFs(f_secs.getPath());
long bytesAvailable = stat.getBlockSizeLong() * stat.getBlockCountLong();
long megAvailable = bytesAvailable / (1024 * 1024);
return megAvailable;
} else {
StatFs stat = new StatFs(f_secs.getPath());
long bytesAvailable = stat.getBlockSize() * stat.getBlockCount();
long megAvailable = bytesAvailable / (1024 * 1024);
return megAvailable;
}
}