如何查找 Android 上剩余的可用存储空间(磁盘空间)?
我试图找出运行我的应用程序的 Android 手机上的可用磁盘空间。有没有办法以编程方式执行此操作?
谢谢,
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?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
由于 blocksize 和 getAvailableBlocks
已被弃用
,因此可以使用
基于 user802467 的上述答案的注释,
我们可以使用
getAvailableBlocksLong
和getBlockSizeLong
Since blocksize and getAvailableBlocks
are deprecated
this code can be use
note based above answer by user802467
we can use
getAvailableBlocksLong
andgetBlockSizeLong
内存位置:
用法
你可以使用这个
Memory Locations:
usage
You can use this
示例:获取人类可读大小,如 1 Gb
String memory = bytesToHuman(totalMemory())
将字节转换为人类可读格式(如 1 Mb、1 Gb)
Example: Getting human readable size like 1 Gb
String memory = bytesToHuman(totalMemory())
Converting bytes to human readable format (like 1 Mb, 1 Gb)
尝试 StatFs.getAvailableBlocks。您需要使用 getBlockSize 将块计数转换为 KB。
Try StatFs.getAvailableBlocks. You'll need to convert the block count to KB with getBlockSize.
目前的答案都没有涉及有关路径的一些微妙之处。您必须根据您感兴趣的统计数据类型使用正确的路径。基于对 DeviceStorageMonitorService.java 的深入研究,它在通知区域中生成磁盘空间不足警告以及 ACTION_DEVICE_STORAGE_LOW 的粘性广播,以下是一些路径您可以使用:
要检查可用的内部磁盘空间,请使用通过Environment.getDataDirectory() 获取的数据目录。这将为您提供数据分区上的可用空间。数据分区托管设备上所有应用程序的所有内部存储。
要检查可用外部 (SDCARD) 磁盘空间,请使用通过 Environment.getExternalStorageDirectory() 获取的外部存储目录。这将为您提供 SDCARD 上的可用空间。
要检查包含操作系统文件的系统分区上的可用内存,请使用Environment.getRootDirectory()。由于您的应用程序无法访问系统分区,因此此统计信息可能不是很有用。 DeviceStorageMonitorService 用于提供信息并将其输入日志中。
要检查临时文件/缓存内存,请使用Environment.getDownloadCacheDirectory()。当内存不足时,DeviceStorageMonitorService 会尝试清理一些临时文件。
用于获取内部(/data)、外部(/sdcard)和操作系统(/system)可用内存的一些示例代码:
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:
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.
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.
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.
To check for temporary files / cache memory, use Environment.getDownloadCacheDirectory(). DeviceStorageMonitorService attempts to clean some of the temporary files when memory gets low.
Some sample code for getting the internal (/data), external (/sdcard) and OS (/system) free memory:
根据@XXX的回答,我创建了一个包装 StatFs 的要点代码片段,以便于使用。
您可以此处作为 GitHub 要点找到它。
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.
在进行乘法之前,将整数值类型转换为 long。两个大整数之间的乘法可能会溢出并给出负结果。
Typecast your integer values to long before doing multiplication. Multiplication between two big integers could overflow and give a negative result.
StatFs
-class 是:示例如下:此处和此处:
上述代码的来源(Wayback Machine)
The
StatFs
-class is:Examples are here and here:
Source for code above (Wayback Machine)