首页 > 代码库 > Android 推断SD卡是否存在及容量查询

Android 推断SD卡是否存在及容量查询

首先先要加入权限
<uses-permission android :name ="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android: name="android.permission.WRITE_EXTERNAL_STIRAGE"/>


推断SD卡是否存在

	/*
	 * 推断SD卡是否存在
	 */
	private boolean ExitSDcard() {
		if (Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_UNMOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

<span style="white-space:pre">	</span>/*
	 * 查看SD卡总容量
	 */
	@SuppressWarnings("deprecation")
	public long getSDAllSize() {
		String path = Environment.getExternalStorageDirectory().getPath();
		StatFs sf = new StatFs(path);
		int blockSize = sf.getBlockSize();

		int allBlocks = sf.getBlockCount();
		return (allBlocks * blockSize) / 1024 / 1024;

	}
/*
	 * 
	 * 查看SD卡剩余空间
	 */
	@SuppressWarnings("deprecation")
	public long getSDFreeSize() {
		String path = Environment.getExternalStorageDirectory().getPath();
		StatFs statFs = new StatFs(path);
		int size = statFs.getBlockSize();
		int freeBlocks = statFs.getAvailableBlocks();
		return (freeBlocks * size) / 1024 / 1024;
	}



Android 推断SD卡是否存在及容量查询