android 获取存储空间信息

本文介绍了一个用于获取Android设备内部存储空间和SD卡存储空间大小的代码片段,包括内部存储的可用和总空间,以及SD卡的可用和总空间。通过检查设备的外部存储状态来实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

该代码片段可以让我们获取internal和external的存储空间大小。 

import java.io.File;                                                                                                

import android.os.Environment;    
import android.os.StatFs;    
    
public class StorageUtil {

    private static final int ERROR = -1;

    /**
     * SDCARD是否存
     */
    public static boolean externalMemoryAvailable() {
        return android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);
    }

    /**
     * 获取手机内部剩余存储空间
     * @return
     */
    public static long getAvailableInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        return availableBlocks * blockSize;
    }

    /**
     * 获取手机内部总的存储空间
     * @return
     */
    public static long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }

    /**
     * 获取SDCARD剩余存储空间
     * @return
     */
    public static long getAvailableExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long availableBlocks = stat.getAvailableBlocks();
            return availableBlocks * blockSize;
        } else {
            return ERROR;
        }
    }

    /**
     * 获取SDCARD总的存储空间
     * @return
     */
    public static long getTotalExternalMemorySize() {
        if (externalMemoryAvailable()) {
            File path = Environment.getExternalStorageDirectory();
            StatFs stat = new StatFs(path.getPath());
            long blockSize = stat.getBlockSize();
            long totalBlocks = stat.getBlockCount();
            return totalBlocks * blockSize;
        } else {
            return ERROR;
        }
    }
}

1.硬件上的 block size, 应该是"sector size",linux的扇区大小是512byte

2.有文件系统的分区的block size, 是"block size",大小不一,可以用工具查看
3.没有文件系统的分区的block size,也叫“block size”,大小指的是1024 byte
4.Kernel buffer cache 的block size, 就是"block size",大部分PC是1024
5.磁盘分区的"cylinder size",用fdisk 可以查看。

      我们这里的block size是第二种情况,一般SD卡都是fat32的文件系统,block size是4096.

      这样就可以知道手机的内部存储空间和sd卡存储空间的总大小和可用大小了。

文章出处:http://blog.163.com/hero_213/blog/static/39891214201162123236660/

获取手机存储空间值 private String getInternalMemoryPath() { return Environment.getDataDirectory().getPath(); } /** * * @return 内置sd卡路径 */ private String getExternalMemoryPath() { // return Environment.getExternalStorageDirectory().getPath(); return "/mnt/sdcard"; } /** * * @return 外置sd卡路径 */ private String getSDCard2MemoryPath() { return "/mnt/sdcard1"; } /** * * @param path * 文件路径 * @return 文件路径的StatFs对象 * @throws Exception * 路径为空或非法异常抛出 */ private StatFs getStatFs(String path) { try { return new StatFs(path); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @param stat * 文件StatFs对象 * @return 剩余存储空间的MB数 * */ private float calculateSizeInMB(StatFs stat) { if (stat != null) return stat.getAvailableBlocks() * (stat.getBlockSize() / (1024f * 1024f)); return 0.0f; } /** * * @return ROM剩余存储空间的MB数 */ private float getAvailableInternalMemorySize() { String path = getInternalMemoryPath();// 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); } /** * * @return 内置SDCard剩余存储空间MB数 */ private float getAvailableExternalMemorySize() { String path = getExternalMemoryPath();// 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); } /** * * @return 外置SDCard剩余存储空间MB数 */ private float getAvailableSDCard2MemorySize() { // String status = Environment.getExternalStorageState(); // if (status.equals(Environment.MEDIA_MOUNTED)) { // } String path = getSDCard2MemoryPath(); // 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值