获取手机SD卡路径(国产神机多个SD卡)
时间:2014-06-22 13:34:25
收藏:0
阅读:418
通过系统的
Environment.getExternalStorageDirectory().getAbsoluteFile();
只能得到系统的SD卡路径,对于对个SD卡的国产神机,想得到外部SD卡就无能为力了。
下面介绍一个编写的工具类,通过反射得到系统隐藏的得到所有挂载路径方法获取所有SD卡路径:
package com.itheima.mobilesafe.utils;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;import android.content.Context;import android.os.storage.StorageManager;public class ExternalMemoryUtils {/*** 得到所有的外部存储路径,以集合的形式返回** @param context* 上下文* @return SD卡路径集合*/public static List<String> getExternalMemoryPaths(Context context) {List<String> list = null;StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);try {Class<?>[] paramClasses = {};Method getVolumePathsMethod = StorageManager.class.getMethod("getVolumePaths", paramClasses);// 返回所有安装卷路径列表getVolumePathsMethod.setAccessible(true);Object[] params = {};Object invoke = getVolumePathsMethod.invoke(storageManager, params);list = new ArrayList<String>();for (int i = 0; i < ((String[]) invoke).length; i++) {String path = ((String[]) invoke)[i];list.add(path);}} catch (IllegalArgumentException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}return list;}}
获取手机SD卡路径(国产神机多个SD卡),布布扣,bubuko.com
原文:http://www.cnblogs.com/loveandroid/p/3801596.html
评论(0)