欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android系统存储信息的获取

程序员文章站 2022-05-02 20:04:00
...

android系统容量信息用Environment和StaFs两个类来获取,具体方法如下:

1.Environment

        File data = Environment.getDataDirectory();//获取手机存储文件对象

        File data = Environment.getExternalStorageDirectory();//获取sdcard文件对象

        File data = Environment.getExternalStorageDirectory();//获取系统root文件对象

2.StatFs:

        StatFs sdStatFs = new StatFs(data.getPath());

        int availableBlocks = statFs.getAvailableBlocks();// 可用存储块的数量
        int blockCount = statFs.getBlockCount();// 总存储块的数量
        int size = statFs.getBlockSize();// 每块存储块的大小
        int totalSize = blockCount * size;// 总存储量
        int availableSize = availableBlocks * size;// 可用容量

 // 获取本机容量信息
	public String phoneCapacity() {
		// 获取本机信息
		File data = Environment.getDataDirectory();
		StatFs statFs = new StatFs(data.getPath());
		int availableBlocks = statFs.getAvailableBlocks();// 可用存储块的数量
		int blockCount = statFs.getBlockCount();// 总存储块的数量

		int size = statFs.getBlockSize();// 每块存储块的大小

		int totalSize = blockCount * size;// 总存储量

		int availableSize = availableBlocks * size;// 可用容量

		String phoneCapacity = Integer.toString(availableSize / 1024 / 1024)
				+ "MB/" + Integer.toString(totalSize / 1024 / 1024) + "MB";

		return phoneCapacity;
	}

	// 获取sdcard容量信息
	public String sdcardCapacity() {
		// 获取sdcard信息
		File sdData = Environment.getExternalStorageDirectory();
		StatFs sdStatFs = new StatFs(sdData.getPath());

		int sdAvailableBlocks = sdStatFs.getAvailableBlocks();// 可用存储块的数量
		int sdBlockcount = sdStatFs.getBlockCount();// 总存储块的数量
		int sdSize = sdStatFs.getBlockSize();// 每块存储块的大小
		int sdTotalSize = sdBlockcount * sdSize;
		int sdAvailableSize = sdAvailableBlocks * sdSize;

		String sdcardCapacity = Integer.toString(sdAvailableSize / 1024 / 1024)
				+ "MB/" + Integer.toString(sdTotalSize / 1024 / 1024) + "MB";
		return sdcardCapacity;
	}