android 获取文件路径(内置sd卡和外置sd卡)。


最近在做选择文件存放目录的功能,这个在项目中应该会被经常运用到的功能。

首先,需要获取到的是文件的路径,但手机又外置和内置sd卡,他们在不同的手机上的挂载目录又不同,具体请移步........。

思路:通过java的反射机制调用系统(android.os)里的StroageManager类来获取所有的sd卡挂载点,以及他们的状态(state)内存,剩余内存、可用内存·········

再通过isRemovable(sd卡是否可被移除)来判断是内置sd卡还是外置sd卡。下面代码就是实现,来自黑月神话获取Android设备挂载的所有存储器,转载时一定注明:

黑月神话获取Android设备挂载的所有存储器

记得加权限

 <!-- sdcard读权限 -->  
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>  
    <!-- sdcard写权限 -->  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


package com.yue.reflact;

import java.io.File;
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;

/**
 * 通过java的反射机制获取android设备上所有存储设备的挂载点
 * @ClassName: StorageInfo 
 * @Description: TODO 
 * @date 2016-5-3 下午5:07:37 
 *
 */
public class StorageInfo {
	public String path;//路径
	public String state;//状态
	public boolean isRemoveable;//是否可移除,可以移除的为外置sd卡,不可以移除的为外置sd卡

	public StorageInfo(String path) {
		this.path = path;
	}

	/*
	 * 是否挂载
	 */
	public boolean isMounted() {
		return "mounted".equals(state);
	}
	
	/**
	 * 获取所有存储设备的挂载点目录
	 * @Title: listAvaliableStorage 
	 * @param context
	 * @return 
	 * List 
	 * @author shimy
	 * @since 2016-5-3 V 1.0
	 */
	 public static List listAvaliableStorage(Context context) {
	        ArrayList storagges = new ArrayList();
	        StorageManager storageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
	        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) {
	                StorageInfo info = null;
	                for (int i = 0; i < invokes.length; i++) {
	                    Object obj = invokes[i];
	                    Method getPath = obj.getClass().getMethod("getPath", new Class[0]);
	                    String path = (String) getPath.invoke(obj, new Object[0]);
	                    info = new StorageInfo(path);
	                    File file = new File(info.path);
	                    if ((file.exists()) && (file.isDirectory()) && (file.canWrite())) {
	                        Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
	                        String state = null;
	                        try {
	                            Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
	                            state = (String) getVolumeState.invoke(storageManager, info.path);
	                            info.state = state;
	                        } catch (Exception e) {
	                            e.printStackTrace();
	                        }

	                        if (info.isMounted()) {
	                            info.isRemoveable = ((Boolean) isRemovable.invoke(obj, new Object[0])).booleanValue();
	                            storagges.add(info);//将挂载设备的信息放入list
	                        }
	                        /*
	                         * 判断存储设备的挂载状态
	                         */
	                        Method getVolumeState = StorageManager.class.getMethod("getVolumeState", String.class);
	    	                state = (String) getVolumeState.invoke(storageManager, info.path);
	    	                info.state = state;
	                    }
	                    /*
	                     * 判断是内置还是外置sd卡
	                     */
	                    Method isRemovable = obj.getClass().getMethod("isRemovable", new Class[0]);
	                }
	                
	            }
	        } catch (NoSuchMethodException e1) {
	            e1.printStackTrace();
	        } catch (IllegalArgumentException e) {
	            e.printStackTrace();
	        } catch (IllegalAccessException e) {
	            e.printStackTrace();
	        } catch (InvocationTargetException e) {
	            e.printStackTrace();
	        }
	        storagges.trimToSize();

	        return storagges;
	    }
}


你可能感兴趣的:(android 获取文件路径(内置sd卡和外置sd卡)。)