(Simplified) Android 9.0 ~ 12 get system memory size, storage space size

Both units are "G", adjust according to your needs. 

/**
     * 总内存
     *
     * @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";
    }

Guess you like

Origin blog.csdn.net/weixin_44917215/article/details/129461097