Android获取可用内存(系统,sd卡,u盘)

/**
*通过反射获取不同存储卡的路径,主要是用来获取外挂sd卡路径,内置sd卡路径可通过Environment类的方法获得
*/
public static boolean hasEnoughStorage(Context context) {
//
        StorageManager storageManager = (StorageManager) context.getSystemService
                (context.STORAGE_SERVICE);
        boolean hasEnoughStorage = false;
        try {
            String[] paths = (String[]) storageManager.getClass
                    ().getMethod("getVolumePaths").invoke(storageManager);
            for (String s : paths) {
                StatFs statFs = new StatFs(s);
                //得到分区大小
                long blockSize = statFs.getBlockSize();
                //得到可用分区数
                long availableBlocks = statFs.getAvailableBlocks();
                //可用分区大小,单位kb
                long availableStorage = (blockSize * availableBlocks) / 1024;//kb
                if (availableStorage > 100) {
                    hasEnoughStorage = true;
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return hasEnoughStorage;
    }

你可能感兴趣的:(学习)