是否可以通过Android SDK获取Android设备(而不是SD卡上)的可用内存量?
若然,怎么样?
答案 0 :(得分:64)
this帖子可能适合您的问题。
还要检查this thread。 SO上有这么多信息。
搜索了一下,这是解决方案(在android git找到)
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(this, availableBlocks * blockSize);
答案 1 :(得分:24)
/*************************************************************************************************
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 = ( (long) statFs.getBlockCount() * (long) statFs.getBlockSize());
return Total;
}
public long FreeMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long Free = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize());
return Free;
}
public long BusyMemory()
{
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long Total = ((long) statFs.getBlockCount() * (long) statFs.getBlockSize());
long Free = (statFs.getAvailableBlocks() * (long) statFs.getBlockSize());
long Busy = Total - Free;
return Busy;
}
将字节转换为人类可读格式(如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 "???";
}
答案 2 :(得分:9)
看起来StatFs
类可能就是您需要使用的类。我不确定哪条路径会被视为设备的根目录,但我相信无论目录如何,结果都是相同的,只要它是内部存储的一部分。这样的事情可能有用:
StatFs stats = new StatFs("/data");
int availableBlocks = stats.getAvailableBlocks();
int blockSizeInBytes = stats.getBlockSize();
int freeSpaceInBytes = availableBlocks * blockSizeInBytes;
如果不出意外,StatFs课程应该为你提供一个良好的开端。
答案 3 :(得分:5)
自2013年以来,Google已弃用某些方法,您可能会更改这些方法(仅限API 18+):
getAvailableBlocks()
至getAvailableBlocksLong()
getBlockCount()
至getBlockCountLong()
getBlockSize()
至getBlockSizeLong()
getFreeBlocks()
至getFreeBlocksLong()
答案 4 :(得分:2)
在内存大小非常大的设备上,由于int值太小,它不起作用。例如在Motorola xum上它不起作用。你必须使用这样的东西:
int freeSpaceInKilobytes = availableBlocks * (blockSizeInBytes / 1024);
答案 5 :(得分:2)
/**
* @return Number of bytes available on internal storage
*/
public static long getInternalAvailableSpace() {
long availableSpace = -1L;
try {StatFs stat = new StatFs(Environment.getDataDirectory()
.getPath());
stat.restat(Environment.getDataDirectory().getPath());
availableSpace = (long) stat.getAvailableBlocks() * (long) stat.getBlockSize();
} catch (Exception e) {
e.printStackTrace();
}
return availableSpace;
}
答案 6 :(得分:0)
请参阅此链接:Android get free size of internal/external memory 这可能有所帮助。在这里,一切都以一种很好的方式解释。