Android获取sdcard信息

 

1>实例代码:

 

		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			File path = Environment.getExternalStorageDirectory();

			StatFs statFs = new StatFs(path.getPath());
			long blockSize = statFs.getBlockSize();
			long totalBlocks = statFs.getBlockCount();
			long availableBlocks = statFs.getAvailableBlocks();

			String[] total = fileSize(totalBlocks * blockSize);
			String[] available = fileSize(availableBlocks * blockSize);

			int ss = (int) (((totalBlocks-availableBlocks) / (totalBlocks * 1.0)) * myProgressBar.getMax());

			myProgressBar.setProgress(ss);
			String text = available[0] + available[1] + "可用 \t总" + total[0] + total[1];
			
			myTextView.setText(text);

		} else if (Environment.getExternalStorageState().equals(Environment.MEDIA_REMOVED)) {
			String text = "SD CARD 已删除";
			myTextView.setText(text);
		}

 

2>获取sdcard状态

 

Environment.getExternalStorageState()

//获得扩展存储设备状态

MEDIA_BAD_REMOVAL//安全卸载前强制拔除
MEDIA_CHECKING//正在扫描
MEDIA_MOUNTED//已挂载
MEDIA_MOUNTED_READ_ONLY//只读
MEDIA_NOFS//sdcar是空的,或文件系统不支持
MEDIA_REMOVED//已移除
MEDIA_SHARED//未挂载,并以USB模式和计算机连接
MEDIA_UNMOUNTABLE//有sdcard,但无法挂载
MEDIA_UNMOUNTED//有sdcard,但未挂载

 

 

 

StringMEDIA_BAD_REMOVALgetExternalStorageState() returns MEDIA_BAD_REMOVAL if the media was removed before it was unmounted.
StringMEDIA_CHECKINGgetExternalStorageState() returns MEDIA_CHECKING if the media is present and being disk-checked
StringMEDIA_MOUNTEDgetExternalStorageState() returns MEDIA_MOUNTED if the media is present and mounted at its mount point with read/write access.
StringMEDIA_MOUNTED_READ_ONLYgetExternalStorageState() returns MEDIA_MOUNTED_READ_ONLY if the media is present and mounted at its mount point with read only access.
StringMEDIA_NOFSgetExternalStorageState() returns MEDIA_NOFS if the media is present but is blank or is using an unsupported filesystem
StringMEDIA_REMOVEDgetExternalStorageState() returns MEDIA_REMOVED if the media is not present.
StringMEDIA_SHAREDgetExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.
StringMEDIA_UNMOUNTABLEgetExternalStorageState() returns MEDIA_UNMOUNTABLE if the media is present but cannot be mounted.
StringMEDIA_UNMOUNTEDgetExternalStorageState() returns MEDIA_UNMOUNTED if the media is present but not mounted.

 

 

3>获取sdcard状态

File path = Environment.getExternalStorageDirectory();//获得sdcard路径
StatFs statFs = new StatFs(path.getPath());
long blockSize = statFs.getBlockSize();//获取块大小
long totalBlocks = statFs.getBlockCount();//获取总块数
long availableBlocks = statFs.getAvailableBlocks();//获取可用块数

long totalSize = totalBlocks*blockSize;//总大小
long availableSize = availableBlocks*blockSize;//可用大小

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值