android5.1判断外置SD卡是否存在
来源:互联网 发布:仿真分析软件 编辑:程序博客网 时间:2024/06/05 06:31
最近发现以前网上搜索的判断外置SD是否存在的方法在android5.1上不能使用了,上网和看源码找到了一个方法可以判断SD卡是否存在。
private boolean isExternalStorageMounted() { final StorageVolume[] volumes = mStorageManager.getVolumeList(); for (StorageVolume v : volumes) { if (v.isRemovable()) { if (Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(v.getPathFile()))) return true; } } return false;}
首先要在oncreate方法里初始化mStorageManager网上别人的文档:mStorageManager = StorageManager.from(this);
此方法我验证过,可以使用。通过正规api得不到外卡路径.
谷歌在源码中给出了得到外卡路径的方法,但标记为隐藏接口,因此api无法访问.
可以通过反射接口得到:
import java.lang.reflect.Method;import android.os.storage.StorageManager; public String getPrimaryStoragePath() { try { StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE); Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null); String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null); // first element in paths[] is primary storage path return paths[0]; } catch (Exception e) { Log.e(TAG, "getPrimaryStoragePath() failed", e); } return null; } public String getSecondaryStoragePath() { try { StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE); Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", null); String[] paths = (String[]) getVolumePathsMethod.invoke(sm, null); // second element in paths[] is secondary storage path return paths[1]; } catch (Exception e) { Log.e(TAG, "getSecondaryStoragePath() failed", e); } return null; } public String getStorageState(String path) { try { StorageManager sm = (StorageManager) getSystemService(STORAGE_SERVICE); Method getVolumeStateMethod = StorageManager.class.getMethod("getVolumeState", new Class[] {String.class}); String state = (String) getVolumeStateMethod.invoke(sm, path); return state; } catch (Exception e) { Log.e(TAG, "getStorageState() failed", e); } return null; }
如果楼主有源码,可以去找StorageManager这个类,拉到文件最下方,就可以看到那三个隐藏接口.
0 0
- android5.1判断外置SD卡是否存在
- Android判断是否存在外置SD卡(获取手机所有存储设备的路径)
- 判断SD卡是否存在
- 判断SD卡是否存在
- android得到外置sd卡路径和判断外置sd卡是否卸载
- Android判断是否挂载外置sd卡
- android 判断SD卡是否存在
- 判断SD卡中的视频文件是否存在
- android得到外置sd卡路径和判断外置sd卡是否卸载,已经验证20个机型左右
- Android5.0 外置SD卡写入权限解决方案
- Android判断获取内置外置sd卡
- 判断手机有无外置sd卡
- Android 判断是否有外置SD卡(包含手机存储的情况下)
- Android判断SD卡是否存在的实现
- Android判断SD卡是否存在的实现
- Android判断SD卡是否存在的实现
- Android 判断SD卡是否存在及容量查询
- Android 判断SD卡是否存在及容量查询
- C++ 长整型数据的输出
- Stub方式接口化封装axis2 webservice:实现衍生对象与原始对象的相互转换
- 树莓派与超声波测距
- Reference VS Pointer
- Windows 64位下安装MySQL
- android5.1判断外置SD卡是否存在
- 一段解决了好多天疑惑的代码
- 线程同步
- Nginx和Apache的区别
- 测试基础知识总结
- mac创建公钥私钥
- 自动生成Android屏幕适配的dimens.xml
- poj 2208
- C++学习记录之vector容器