我是靠谱客的博主 开放故事,最近开发中收集的这篇文章主要介绍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卡已拔出

}

}

};

最后

以上就是开放故事为你收集整理的android tf自动挂载,Android判断是否挂载外置SD/TF卡的全部内容,希望文章能够帮你解决android tf自动挂载,Android判断是否挂载外置SD/TF卡所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(50)

评论列表共有 0 条评论

立即
投稿
返回
顶部