安卓学习笔记获取手机内存与SD卡路径及大小

实现原理

  • 通过反射,调用StorageManger被隐藏接口getVolumePaths(),来获得手机的存储路径,进而通过StatFs获取区块blockCount与blockSize的大小,以计算出内存值
  • 在使用getBlockCountLong(),getBlockSizeLong(),getAvailableBlocks()时要注意api>=18;

方法代码

/** * 获取ROM总大小 * @param context * @return */
    public static String getMaxRomSpace(Context context) {
        StatFs fs = new StatFs(getInnerSDCardPath(context));
        String romSpace; 
        if (Build.VERSION.SDK_INT  >=18) {
            long blockCount = fs.getBlockCountLong();
            long bloackSize = fs.getBlockSizeLong();
            long totalSpace = bloackSize*blockCount;
            romSpace = Formatter.formatFileSize(context, totalSpace);
        }else{
            long blockCount = fs.getBlockCount();
            long bloackSize = fs.getBlockSize();
            long totalSpace = bloackSize*blockCount;
            romSpace = Formatter.formatFileSize(context, totalSpace);
        }
        return romSpace;
    }
/** * 获取SD卡总大小 * @param context * @return */
    public static String getMaxSDSpace(Context context) {
        String extPath = getExtSDCardPath(context);
        if (TextUtils.isEmpty(extPath)) {
            Toast.makeText(context, "无SD卡", 0).show();
            return null;
        }
        File path = new File(extPath);
        StatFs fs = new StatFs(path.getPath());
        String sdSpace;
        if (Build.VERSION.SDK_INT  >=18) {
            long blockCount = fs.getBlockCountLong();
            long bloackSize = fs.getBlockSizeLong();
            long totalSpace = bloackSize*blockCount;
            sdSpace = Formatter.formatFileSize(context, totalSpace);
        }else{
            long blockCount = fs.getBlockCount();
            long bloackSize = fs.getBlockSize();
            long totalSpace = bloackSize*blockCount;
            sdSpace = Formatter.formatFileSize(context, totalSpace);
        }
        return sdSpace;
    }
/** * 获取rom可用大 * 小 * @param context * @return */
    public static String getAvailableRomSpace(Context context) {
        File path = Environment.getDataDirectory();
        StatFs fs = new StatFs(path.getPath());
        String Space = null ;
        if (Build.VERSION.SDK_INT  >=18) {

            long blockCount = fs.getAvailableBlocksLong();
            long bloackSize = fs.getBlockSizeLong();
            long totalSpace = bloackSize*blockCount;
            Space = Formatter.formatFileSize(context, totalSpace);
        }else{
            long blockCount = fs.getAvailableBlocks();
            long bloackSize = fs.getBlockSize();
            long totalSpace = bloackSize*blockCount;
            Space = Formatter.formatFileSize(context, totalSpace);
        }
        return Space;
    }

    /** * 获取SD卡可用大小 * @param context * @return */
    public static String getAvailableSdSpace(Context context) {
        String extPath = getExtSDCardPath(context);
        if (TextUtils.isEmpty(extPath)) {
            Toast.makeText(context, "无SD卡", 0).show();
            return null;
        }
        File path = new File(extPath);
        StatFs fs = new StatFs(path.getPath());
        String Space;
        if (Build.VERSION.SDK_INT >=18) {
            long blockCount = fs.getAvailableBlocksLong();
            long bloackSize = fs.getBlockSizeLong();
            long totalSpace = bloackSize*blockCount;
            Space = Formatter.formatFileSize(context, totalSpace);
        }else{
            long blockCount = fs.getAvailableBlocks();
            long bloackSize = fs.getBlockSize();
            long totalSpace = bloackSize*blockCount;
            Space = Formatter.formatFileSize(context, totalSpace);
        }
        return Space;
    }
    /** * 获取外置SD卡路径 * @return 应该就一条记录或空 */
    public static String getExtSDCardPath(Context context){
        StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        try {
            getPathsMethod = sm.getClass().getMethod("getVolumePaths", null);
            String[] path = (String[]) getPathsMethod.invoke(sm, null);
            return path[1];
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
    /** * 获取内置内存路径 * @return 应该就一条记录或空 */
    public static String getInnerSDCardPath(Context context){
        StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);
        try {
            getPathsMethod = sm.getClass().getMethod("getVolumePaths", null);
            String[] path = (String[]) getPathsMethod.invoke(sm, null);
            return path[0];
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

你可能感兴趣的:(android,存储)