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";
    }
获取手机存储空间值 private String getInternalMemoryPath() { return Environment.getDataDirectory().getPath(); } /** * * @return 内置sd卡路径 */ private String getExternalMemoryPath() { // return Environment.getExternalStorageDirectory().getPath(); return "/mnt/sdcard"; } /** * * @return 外置sd卡路径 */ private String getSDCard2MemoryPath() { return "/mnt/sdcard1"; } /** * * @param path * 文件路径 * @return 文件路径的StatFs对象 * @throws Exception * 路径为空或非法异常抛出 */ private StatFs getStatFs(String path) { try { return new StatFs(path); } catch (Exception e) { e.printStackTrace(); } return null; } /** * * @param stat * 文件StatFs对象 * @return 剩余存储空间的MB数 * */ private float calculateSizeInMB(StatFs stat) { if (stat != null) return stat.getAvailableBlocks() * (stat.getBlockSize() / (1024f * 1024f)); return 0.0f; } /** * * @return ROM剩余存储空间的MB数 */ private float getAvailableInternalMemorySize() { String path = getInternalMemoryPath();// 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); } /** * * @return 内置SDCard剩余存储空间MB数 */ private float getAvailableExternalMemorySize() { String path = getExternalMemoryPath();// 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); } /** * * @return 外置SDCard剩余存储空间MB数 */ private float getAvailableSDCard2MemorySize() { // String status = Environment.getExternalStorageState(); // if (status.equals(Environment.MEDIA_MOUNTED)) { // } String path = getSDCard2MemoryPath(); // 获取数据目录 StatFs stat = getStatFs(path); return calculateSizeInMB(stat); }
获取内存大小,可以获取总内存,剩余内存,以及已经使用的内存 文件目录: CheckMemory ...........\.idea ...........\.....\.name ...........\.....\androidDexCompiler.xml ...........\.....\compiler.xml ...........\.....\copyright ...........\.....\.........\profiles_settings.xml ...........\.....\encodings.xml ...........\.....\misc.xml ...........\.....\modules.xml ...........\.....\scopes ...........\.....\......\scope_settings.xml ...........\.....\uiDesigner.xml ...........\.....\vcs.xml ...........\.....\workspace.xml ...........\AndroidManifest.xml ...........\ant.properties ...........\assets ...........\bin ...........\build.xml ...........\CheckMemory.iml ...........\gen ...........\...\me ...........\...\..\normalhh ...........\...\..\........\checkmemory ...........\...\..\........\...........\BuildConfig.java ...........\...\..\........\...........\Manifest.java ...........\...\..\........\...........\R.java ...........\libs ...........\local.properties ...........\out ...........\...\production ...........\...\..........\CheckMemory ...........\...\..........\...........\CheckMemory.apk ...........\...\..........\...........\CheckMemory.unaligned.apk ...........\...\..........\...........\me ...........\...\..........\...........\..\normalhh ...........\...\..........\...........\..\........\checkmemory ...........\...\..........\...........\..\........\...........\BuildConfig.class ...........\...\..........\...........\..\........\...........\MyActivity.class ...........\...\..........\...........\..\........\...........\R$attr.class ...........\...\..........\...........\..\........\...........\R$drawable.class ...........\...\..........\...........\..\........\...........\R$id.class ...........\...\..........\...........\..\........\...........\R$layout.class ...........\...\..........\...........\..\........\...........\R$string.class ...........\...\..........\...........\..\........\...........\R.class ...........\proguard-project.txt ...........\project.properties ...........\res ...........\...\drawable-hdpi ...........\...\.............\ic_launcher.png ...........\...\drawable-ldpi ...........\...\.............\ic_launcher.png ...........\...\drawable-mdpi ...........\...\.............\ic_launcher.png ...........\...\drawable-xhdpi ...........\...\..............\ic_launcher.png ...........\...\layout ...........\...\......\main.xml ...........\...\values ...........\...\......\strings.xml ...........\src ...........\...\me ...........\...\..\normalhh ...........\...\..\........\checkmemory ...........\...\..\........\...........\MyActivity.java
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaowang_lj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值