获取手机和SD卡路径(Android 4.0以上验证过)
2014-04-03 17:31
309 查看
1. android 系统内部使用
StorageManager mStorageManager = (StorageManager)getApplicationContext().getSystemService("storage");
StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
for (StorageVolume volume : storageVolumes) {
if(volume.isRemovable()){
//sdcard
volume.getPath();//外部SD卡存储路径
}else if(volume.isEmulated()){
//phone
volume.getPath();//手机内部存储路径
}
}
2. 第三方使用(使用反射方式)
private void testStorage(){
StorageManager storageManager = (StorageManager)getApplicationContext().getSystemService("storage");
try {
Class<?>[] paramClasses = {};
Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
getVolumeList.setAccessible(true);
Object[] params = {};
Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
if (invokes != null) {
for (int i = 0; i < invokes.length; i++) {
Object obj = invokes[i];
Method getPath = obj.getClass().getMethod("getPathFile", new Class[0]);
File file = (File)getPath.invoke(obj, new Object[0]);
if ((file.exists()) && (file.isDirectory())) {
Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
Boolean is_removable = (Boolean)isRemovable.invoke(obj, new Object[0]);
Method getState = obj.getClass().getMethod("getState", new Class[0]);
String storageState = (String)getState.invoke(obj, new Object[0]);
Log.i("","path:"+file.getAbsolutePath()+",State:"+storageState+",isRemovable:"+is_removable);
if(storageState.equals("mounted") || storageState.equals("removed")){
if(is_removable){
Log.i("","外部SD卡存储路径 "+file.getAbsolutePath());
}else{
Log.i("","手机内部存储路径 "+file.getAbsolutePath());
}
}
}
}
}
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
StorageManager mStorageManager = (StorageManager)getApplicationContext().getSystemService("storage");
StorageVolume[] storageVolumes = mStorageManager.getVolumeList();
for (StorageVolume volume : storageVolumes) {
if(volume.isRemovable()){
//sdcard
volume.getPath();//外部SD卡存储路径
}else if(volume.isEmulated()){
//phone
volume.getPath();//手机内部存储路径
}
}
2. 第三方使用(使用反射方式)
private void testStorage(){
StorageManager storageManager = (StorageManager)getApplicationContext().getSystemService("storage");
try {
Class<?>[] paramClasses = {};
Method getVolumeList = StorageManager.class.getMethod("getVolumeList", paramClasses);
getVolumeList.setAccessible(true);
Object[] params = {};
Object[] invokes = (Object[]) getVolumeList.invoke(storageManager, params);
if (invokes != null) {
for (int i = 0; i < invokes.length; i++) {
Object obj = invokes[i];
Method getPath = obj.getClass().getMethod("getPathFile", new Class[0]);
File file = (File)getPath.invoke(obj, new Object[0]);
if ((file.exists()) && (file.isDirectory())) {
Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
Boolean is_removable = (Boolean)isRemovable.invoke(obj, new Object[0]);
Method getState = obj.getClass().getMethod("getState", new Class[0]);
String storageState = (String)getState.invoke(obj, new Object[0]);
Log.i("","path:"+file.getAbsolutePath()+",State:"+storageState+",isRemovable:"+is_removable);
if(storageState.equals("mounted") || storageState.equals("removed")){
if(is_removable){
Log.i("","外部SD卡存储路径 "+file.getAbsolutePath());
}else{
Log.i("","手机内部存储路径 "+file.getAbsolutePath());
}
}
}
}
}
} catch (NoSuchMethodException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
相关文章推荐
- Android手机内置SD卡路径的获取
- Android手机如何获取手机内存路径和sd卡路径
- 关于android4.0以后,SD卡路径获取问题
- android 手机获取外置SD卡路径
- Android获取不同手机 自身存储和外置SD卡存储路径的解决方案
- Android判断是否存在外置SD卡(获取手机所有存储设备的路径)
- Android 4.0获取外置SD卡 路径
- android项目中如何获取assets下面的文件在SD卡上面的保存路径
- 【小功能2】android获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等)
- 获取android 内置与外置SD卡路径及可用空间
- Android小功能获取手机信息(号码,内存,CPU,分辨率,MAC,IP,SD卡,IMEI,经纬度,信号强度等等)
- 获取android多个sd卡路径的方法
- android 获取外置SD卡的路径(非挫版)
- android SD卡路径问题以及如何获取SDCard 内存
- android 获取手机内存及SD卡内存可用空间
- android获取外置SD卡的路径
- Android获取外置SD卡读写路径
- Android 获取屏幕截图 和保存到本地的sd卡路径下
- Android获取SD卡中选中图片的路径(URL)
- Android获取SD卡的路径和内存以及Environment 类的详解