Android 判断SD卡是否存在及容量查询的简单方法如下:
首先要在AndroidManifest.xml中增加SD卡访问权限
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
SD卡是否存在
- private boolean ExistSDCard() {
- 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();
-
-
-
- return (freeBlocks * blockSize)/1024 /1024;
- }
SD卡总容量
- public long getSDAllSize(){
-
- File path = Environment.getExternalStorageDirectory();
- StatFs sf = new StatFs(path.getPath());
-
- long blockSize = sf.getBlockSize();
-
- long allBlocks = sf.getBlockCount();
-
-
-
- return (allBlocks * blockSize)/1024/1024;
- }