Android 9.0 ~ 12 获取系统内存大小、存储空间大小

两者单位均为 “G”,根据自己需要调整。 

/**
     * 总内存
     *
     * @param context 上下文
     * @return {@link String}
     */
    public static String totalMemory(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
        am.getMemoryInfo(mi);
        return String.valueOf((int) Math.ceil((double) mi.totalMem / 1000 / 1000 / 1000));
    }
 
    /**
     * 存储空间
     *
     * @return {@link String}
     */
    public static String totalStorage(Context context) {
        StorageStatsManager storageStatsManager = (StorageStatsManager)context.getSystemService(STORAGE_STATS_SERVICE);
        try {
            long totalBytes = storageStatsManager.getTotalBytes(StorageManager.UUID_DEFAULT); //总空间大小
            return String.valueOf(totalBytes / 1000 / 1000 / 1000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }

你可能感兴趣的:(android)