获取内存上的可用空间
是否可以通过 Android SDK 获取 Android 设备(而不是 SD 卡)上的可用内存量?
如果是这样,怎么办?
Is it possible to get the amount of free memory on an Android device (not on the SD CARD) via the Android SDK?
If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这篇文章可能很适合您的问题。
另请检查此线程。这里有很多关于SO的信息。
谷歌搜索了一下,这里是解决方案(位于 android git)
this post might fit well to your question.
also check this thread. there is so much info here on SO.
googled a bit and here is the solution (found at android git)
将字节转换为人类可读格式(如 1 Mb、1 Gb)
Converting bytes to human readable format (like 1 Mb, 1 Gb)
看起来
StatFs
类可能是你需要使用什么。我不确定哪个路径将被视为设备的根目录,但我相信无论目录如何,只要它是内部存储的一部分,结果都是相同的。像这样的东西可能会起作用:如果不出意外的话,StatFs 类应该给你一个良好的开始,让你知道去哪里寻找。
It looks like the
StatFs
class might be what you need to use. I'm not sure what path would be considered the root of the device, but I believe the result would be the same regardless of directory, as long as it's part of the internal storage. Something like this may work:If nothing else, the StatFs class should give you a good start on where to look.
自 2013 年以来,有些方法已被 Google 弃用,您可以更改这些方法(仅限 API 18+):
getAvailableBlocks()
更改为getAvailableBlocksLong()
getBlockCount( )
到getBlockCountLong()
getBlockSize()
到getBlockSizeLong()
getFreeBlocks()
到getFreeBlocksLong()
There are some methods which were deprecated by Google since 2013 and you might change these methods (API 18+ only):
getAvailableBlocks()
togetAvailableBlocksLong()
getBlockCount()
togetBlockCountLong()
getBlockSize()
togetBlockSizeLong()
getFreeBlocks()
togetFreeBlocksLong()
在内存非常大的设备上,它不起作用,因为 int 值太小。例如,在 Motorola xum 上它不起作用。你必须使用这样的东西:
int freeSpaceInKilobytes = availableBlocks * (blockSizeInBytes / 1024);
On devices where internal memory size is very big it doesn't work because int value is too small. For example on Motorola xum it doesn't work. You have to use something like this:
int freeSpaceInKilobytes = availableBlocks * (blockSizeInBytes / 1024);
请参考此链接:Android 获取内部/外部内存的可用大小
这可能会有所帮助。在这里,一切都得到了很好的解释。
Please refer this link: Android get free size of internal/external memory
This might help. Here, everything is explained in a nice way.