Android 判断SD卡是否存在及容量查询的简单方法如下:
首先要在AndroidManifest.xml中增加SD卡访问权限
Ø 在SDCard中创建与删除文件权限
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Ø 往SDCard写入数据权限
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Ø SD卡是否存在
private booleanExistSDCard() {
if(android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
return true;
} else
return false;
}
Ø SD卡剩余空间
public long getSDFreeSize(){
File path =Environment.getExternalStorageDirectory();//路径
StatFs sf = new StatFs(path.getPath());
long blockSize = sf.getBlockSize();//获取单个数据块的大小
long freeBlocks =sf.getAvailableBlocks();//空闲数据块数量
//返回SD卡空闲大小
//return freeBlocks * blockSize; //单位Byte
//return (freeBlocks *blockSize)/1024; //单位KB
return (freeBlocks * blockSize)/1024/1024; //单位MB
}
Ø SD卡总容量
public longgetSDAllSize(){
File path =Environment.getExternalStorageDirectory();
StatFs sf = newStatFs(path.getPath());
long blockSize = sf.getBlockSize();
long allBlocks = sf.getBlockCount(); //获取所有数据块数
//返回SD卡大小
//return allBlocks * blockSize; //单位Byte
//return (allBlocks * blockSize)/1024; //单位KB
return (allBlocks * blockSize)/1024/1024;//单位MB
}