Android判断是否挂载外置SD/TF卡

如果程序启动前就已经挂载了卡,那么需要使用StorageVolume类的反射来实现!因为广播的方式只能在程序启动后检测卡的插/拔,所以合理的方式应该是同时使用反射和广播。在Android9.0上可能第一种反射方式判断外置SD/TF卡的方式无效,所以这里提供两种。

StorageVolume类:

/**
 * 判断外置sd卡是否挂载
 *
 * @return
 */
private boolean isExistCard() {
	boolean result = false;
	StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
	try {
		Method method1 = StorageManager.class.getMethod("getVolumeList", null);
		method1.setAccessible(true);
		Object[] arrays = (Object[]) method1.invoke(storageManager, null);
		if (arrays != null) {
			for (Object temp : arrays) {
				Method mRemoveable = temp.getClass().getMethod("isRemovable", null);
				Boolean isRemovable = (Boolean) mRemoveable.invoke(temp, null);
				if (isRemovable) {
					Method getPath = temp.getClass().getMethod("getPath", null);
					String path = (String) mRemoveable.invoke(temp, null);
					Method getState = storageManager.getClass().getMethod("getVolumeState", String.class);
					String state = (String) getState.invoke(storageManager, path);
					if (state.equals(Environment.MEDIA_MOUNTED)) {
						result = true;
						break;
					}
				}
			}
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}

/**
 * 判断外置sd卡是否挂载
 *
 * @return
 */
public boolean isExistCard2() {
	boolean result = false;
	StorageManager mStorageManager = (StorageManager) this.getSystemService(Context.STORAGE_SERVICE);
	Class storageVolumeClazz = null;
	try {
		storageVolumeClazz = Class.forName("android.os.storage.StorageVolume");
		Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList");
		Method getPath = storageVolumeClazz.getMethod("getPath");
		Method isRemovable = storageVolumeClazz.getMethod("isRemovable");
		Method getState = storageVolumeClazz.getMethod("getState");
		Object obj = null;
		try {
			obj = getVolumeList.invoke(mStorageManager);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		final int length = Array.getLength(obj);
		for (int i = 0; i < length; i++) {
			Object storageVolumeElement = Array.get(obj, i);
			String path = (String) getPath.invoke(storageVolumeElement);
			boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement);
			String state = (String) getState.invoke(storageVolumeElement);
			if (removable && state.equals(Environment.MEDIA_MOUNTED)) {
				result = true;
				break;
			}
		}
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (InvocationTargetException e) {
		e.printStackTrace();
	} catch (NoSuchMethodException e) {
		e.printStackTrace();
	} catch (IllegalAccessException e) {
		e.printStackTrace();
	}
	return result;
}

广播:

IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
//必须加入否则无法检测sd卡
filter.addDataScheme("file");
//注册广播
registerReceiver(mReceiver, filter);



final BroadcastReceiver mReceiver = new BroadcastReceiver() {
	@Override
	public void onReceive(Context context, Intent intent) {
		if (Objects.equals(intent.getAction(), Intent.ACTION_MEDIA_MOUNTED)) {
			//SD/TF卡已插入
		}
		if (Objects.equals(intent.getAction(), Intent.ACTION_MEDIA_UNMOUNTED)) {
			//SD/TF卡已拔出
		}
	}
};

 

你可能感兴趣的:(Android判断是否挂载外置SD/TF卡)