RK3128-android7.1-emmc实际EMMC与存储显示不匹配

博客详细讨论了Android系统中`FileUtils`和`StorageManager`类的实现,包括`listFilesOrEmpty`和`newFileOrNull`方法,以及新增的`roundStorageSize`方法用于更精确的存储大小计算。此外,还展示了如何获取主要存储卷的大小,替换原有的获取方式,提高了获取存储信息的稳定性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

偶然发现显示不正常

\frameworks\base\core\java\android\os\FileUtils.java

    public static @NonNull File[] listFilesOrEmpty(@Nullable File dir, FilenameFilter filter) {
        if (dir == null) return EMPTY;
        final File[] res = dir.listFiles(filter);
        if (res != null) {
            return res;
        } else {
            return EMPTY;
        }
    }

    public static @Nullable File newFileOrNull(@Nullable String path) {
        return (path != null) ? new File(path) : null;
    }
+	 public static long roundStorageSize(long size) {
+	   long val = 1;
+	   long pow = 1;
+	   while ((val * pow) < size) {
+		   val <<= 1;
+		   if (val > 512) {
+			   val = 1;
+			   pow *= 1024;
+		   }
+	   }
+	   return val * pow;
+	}
}
\frameworks\base\core\java\android\os\storage\StorageManager.java

    public @NonNull StorageVolume getPrimaryStorageVolume() {
        return getVolumeList(UserHandle.myUserId(), FLAG_REAL_STATE | FLAG_INCLUDE_INVISIBLE)[0];
    }

    /** {@hide} */
+	public long getPrimaryStorageSize() {
+	   return FileUtils.roundStorageSize(Environment.getDataDirectory()
+        .getTotalSpace()+Environment.getRootDirectory().getTotalSpace());
+	}
	/*
-    public long getPrimaryStorageSize() {
-        if(alternative_path){
-            for (String path : INTERNAL_STORAGE_SIZE_PATHS_ALTERNATIVE) {
-                final long numberBlocks = readLong(path);
-                if (numberBlocks > 0) {
-                    return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
-                }
-            }
-        }
-        else{
-            for (String path : INTERNAL_STORAGE_SIZE_PATHS) {
-                final long numberBlocks = readLong(path);
-                if (numberBlocks > 0) {
-                    return numberBlocks * INTERNAL_STORAGE_SECTOR_SIZE;
-                }
-            }
-        }
-        return 0;
-   }
*/
    private long readLong(String path) {
        try (final FileInputStream fis = new FileInputStream(path);
                final BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) {
            return Long.parseLong(reader.readLine());
        } catch (Exception e) {
            Slog.w(TAG, "Could not read " + path, e);
            return 0;
        }
    }

 参考博客:

https://blog.youkuaiyun.com/layuetian2011/article/details/110110348

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

旋风旋风

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

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

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

打赏作者

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

抵扣说明:

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

余额充值