关于android获取sd卡路径的方法
2016-01-07 11:36
579 查看
项目中使用到了离线数据,数据库可能比较大,平板本身容量有限,就想到了数据存储到sd卡上。由于android设备五花八门,sdk里面也没有给出明确的获取sd卡的方法,自己网上找了个,测试三星平板目前没问题,方法如下:
/** * 获取sd卡路径 * @param mContext * @param is_removale * @return */ public static String getSDPath(Context mContext, boolean is_removale) { StorageManager mStorageManager = (StorageManager) mContext.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"); Object result = getVolumeList.invoke(mStorageManager); final int length = Array.getLength(result); for (int i = 0; i < length; i++) { Object storageVolumeElement = Array.get(result, i); String path = (String) getPath.invoke(storageVolumeElement); boolean removable = (Boolean) isRemovable.invoke(storageVolumeElement); if (is_removale == removable) { return path; } } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return null; }
相关文章推荐
- Android 插件化 动态升级
- android activity与view的联系--window
- Android中自定义View的MeasureSpec使用
- Android 关于expandableListView childrenView 点击改变颜色
- Android使用Xutils在Entity填充数据
- Android 屏幕适配方案
- 《疯狂Android讲义》
- 教你搞定Android自定义ViewGroup
- vlc_for_android 编译安装(未完成)
- android xml界面布局特殊属性
- 【转】Android Recovery模式
- android 去掉标题栏和全屏,横竖屏强制转换
- Android 版本更新
- Android之TextView(控件)
- Android Fragment与activity交互方式
- Android View与GroupView原理以及其子类描述
- 获取Android屏幕尺寸、控件尺寸、状态栏/通知栏高度、导航栏高度
- Android 自定义标题栏背景
- Android的JS调用Java代码或使用了Javascript相关技术,混淆问题
- 更换android开机动画须选择好正确的压缩格式