欢迎使用CSDN-markdown编辑器

来源:互联网 发布:千手一族 知乎 编辑:程序博客网 时间:2024/06/03 04:45

安卓SD路径获取

今天把一张图片扔到SD卡里,然后去获取文件,就是拿不到。后来想了下,我把图片放到了我自己插入的拓展卡里,然后获取的路径是安卓自带的内存路径,所以读取的文件为空。然后我又把图片重新拷贝了一份到内存中,就OK了。
安卓现在倒是不鼓励在用拓展卡了,6.0当时的新特性中,也有一条就是外接拓展卡可以设置为内存使用。可现在我就想知道,不设置为内存,那路径怎么获取???

- 首先看一下获取内存路径的方法

**Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)**
通过这行代码来判断是否有挂在SD卡。


>

String dir = Environment.getExternalStorageDirectory().getAbsolutePath();
这样就可以获取到SD卡的根目录:/storage/emulated/0


String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC).getAbsolutePath();
这样获取到SD卡的公有目录(具体传入类型,可查看源码):
/storage/emulated/0/Music


String dir = context.getExternalCacheDir().getAbsolutePath();
这样可以获取到应用的私有目录路径:
/storage/emulated/0/Android/data/com.xxx.xxx/cache

这就可以获取到SD卡的常用路径了,剩下的就是自行创建目录来存储文件。

- 获取拓展卡路径

获取拓展卡的路劲获取,我查看了许多博客,大部分都没有效果了(我测试用的手机系统为7.0)。最后找到一篇博客: [ Android6.0如何判断有无外置SD卡(TF卡),并读写 ]

下面我只贴出代码,具体的还请点击上面的博客超链。

public class StorageUtils {    private static final String TAG = StorageUtils.class.getSimpleName();    public static ArrayList<StorageBean> getStorageData(Context pContext) {        final StorageManager storageManager = (StorageManager) pContext.getSystemService(Context.STORAGE_SERVICE);        try {            //得到StorageManager中的getVolumeList()方法的对象            final Method getVolumeList = storageManager.getClass().getMethod("getVolumeList");            //---------------------------------------------------------------------            //得到StorageVolume类的对象            final Class<?> storageValumeClazz = Class.forName("android.os.storage.StorageVolume");            //---------------------------------------------------------------------            //获得StorageVolume中的一些方法            final Method getPath = storageValumeClazz.getMethod("getPath");            Method isRemovable = storageValumeClazz.getMethod("isRemovable");            Method mGetState = null;            //getState 方法是在4.4_r1之后的版本加的,之前版本(含4.4_r1)没有            // (http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/os/Environment.java/)            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {                try {                    mGetState = storageValumeClazz.getMethod("getState");                } catch (NoSuchMethodException e) {                    e.printStackTrace();                }            }            //---------------------------------------------------------------------            //调用getVolumeList方法,参数为:“谁”中调用这个方法            final Object invokeVolumeList = getVolumeList.invoke(storageManager);            //---------------------------------------------------------------------            final int length = Array.getLength(invokeVolumeList);            ArrayList<StorageBean> list = new ArrayList<>();            for (int i = 0; i < length; i++) {                final Object storageValume = Array.get(invokeVolumeList, i);//得到StorageVolume对象                final String path = (String) getPath.invoke(storageValume);                final boolean removable = (Boolean) isRemovable.invoke(storageValume);                String state = null;                if (mGetState != null) {                    state = (String) mGetState.invoke(storageValume);                } else {                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {                        state = Environment.getStorageState(new File(path));                    } else {                        if (removable) {                            state = EnvironmentCompat.getStorageState(new File(path));                        } else {                            //不能移除的存储介质,一直是mounted                            state = Environment.MEDIA_MOUNTED;                        }                        final File externalStorageDirectory = Environment.getExternalStorageDirectory();                        Log.e(TAG, "externalStorageDirectory==" + externalStorageDirectory);                    }                }                long totalSize = 0;                long availaleSize = 0;                if (Environment.MEDIA_MOUNTED.equals(state)) {                    totalSize = StorageUtils.getTotalSize(path);                    availaleSize = StorageUtils.getAvailableSize(path);                }                final String msg = "path==" + path                        + " ,removable==" + removable                        + ",state==" + state                        + ",total size==" + totalSize + "(" + StorageUtils.fmtSpace(totalSize) + ")"                        + ",availale size==" + availaleSize + "(" + StorageUtils.fmtSpace(availaleSize) + ")";                Log.e(TAG, msg);                StorageBean storageBean = new StorageBean();                storageBean.setAvailableSize(availaleSize);                storageBean.setTotalSize(totalSize);                storageBean.setMounted(state);                storageBean.setPath(path);                storageBean.setRemovable(removable);                list.add(storageBean);            }            return list;        } catch (ClassNotFoundException e) {            e.printStackTrace();        } catch (NoSuchMethodException e) {            e.printStackTrace();        } catch (InvocationTargetException e) {            e.printStackTrace();        } catch (IllegalAccessException e) {            e.printStackTrace();        } catch (Exception e) {            e.printStackTrace();        }        return null;    }    public static long getTotalSize(String path) {        try {            final StatFs statFs = new StatFs(path);            long blockSize = 0;            long blockCountLong = 0;            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {                blockSize = statFs.getBlockSizeLong();                blockCountLong = statFs.getBlockCountLong();            } else {                blockSize = statFs.getBlockSize();                blockCountLong = statFs.getBlockCount();            }            return blockSize * blockCountLong;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    public static long getAvailableSize(String path) {        try {            final StatFs statFs = new StatFs(path);            long blockSize = 0;            long availableBlocks = 0;            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {                blockSize = statFs.getBlockSizeLong();                availableBlocks = statFs.getAvailableBlocksLong();            } else {                blockSize = statFs.getBlockSize();                availableBlocks = statFs.getAvailableBlocks();            }            return availableBlocks * blockSize;        } catch (Exception e) {            e.printStackTrace();            return 0;        }    }    public static final long A_GB = 1073741824;    public static final long A_MB = 1048576;    public static final int A_KB = 1024;    public static String fmtSpace(long space) {        if (space <= 0) {            return "0";        }        double gbValue = (double) space / A_GB;        if (gbValue >= 1) {            return String.format("%.2fGB", gbValue);        } else {            double mbValue = (double) space / A_MB;            Log.e("GB", "gbvalue=" + mbValue);            if (mbValue >= 1) {                return String.format("%.2fMB", mbValue);            } else {                final double kbValue = space / A_KB;                return String.format("%.2fKB", kbValue);            }        }    }}