android tf自动挂载,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卡已拔出

}

}

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值