Android9~Android13 某些容量SD卡被格式化为内部存储时容量显示错误问题的研究与解决方案

声明:原创文章,禁止转载!

Android9~Android13 某些容量SD卡被格式化为内部存储时容量显示错误问题的研究与解决方案

分析Android11 系统对于EMMC/UFS作为内部存储、SD卡被格式化为内部存储、SD卡/U盘被格式化为便携式存储的不同处理


一.现象描述

实测Android9 Android10 Android11 Android12 Android13系统中某些容量的SD卡在被格式化为内部存储时,在设置中的显示容量与实际容量不符,比如某些16GB容量的SD卡在设置->存储中显示为32GB,但是如果选择“格式化为便携式存储设备”的话可以正常显示容量为16GB。

在Android11系统格式化为内部存储设备和便携式存储设备

同一个SD卡在Android7系统上作为内部存储和便携式存储空间时显示如下


二.源码分析

Android11系统

packages/apps/Settings/src/com/android/settings/deviceinfo/StorageSettings.java

    @Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);final Context context = getActivity();mStorageManager = context.getSystemService(StorageManager.class);if (sTotalInternalStorage <= 0) {sTotalInternalStorage = mStorageManager.getPrimaryStorageSize();}addPreferencesFromResource(R.xml.device_info_storage);mInternalCategory = (PreferenceCategory) findPreference("storage_internal");mExternalCategory = (PreferenceCategory) findPreference("storage_external");mInternalSummary = new StorageSummaryPreference(getPrefContext());setHasOptionsMenu(true);}private synchronized void refresh() {final Context context = getPrefContext();getPreferenceScreen().removeAll();mInternalCategory.removeAll();mExternalCategory.removeAll();mInternalCategory.addPreference(mInternalSummary);final StorageManagerVolumeProvider smvp = new StorageManagerVolumeProvider(mStorageManager);final PrivateStorageInfo info = PrivateStorageInfo.getPrivateStorageInfo(smvp);final long privateTotalBytes = info.totalBytes;final long privateUsedBytes = info.totalBytes - info.freeBytes;final List<VolumeInfo> volumes = mStorageManager.getVolumes();Collections.sort(volumes, VolumeInfo.getDescriptionComparator());for (VolumeInfo vol : volumes) {if (vol.getType() == VolumeInfo.TYPE_PRIVATE) {if (vol.getState() == VolumeInfo.STATE_UNMOUNTABLE) {mInternalCategory.addPreference(new StorageVolumePreference(context, vol, 0));} else {final long volumeTotalBytes = PrivateStorageInfo.getTotalSize(vol,sTotalInternalStorage);mInternalCategory.addPreference(new StorageVolumePreference(context, vol, volumeTotalBytes));}} else if (vol.getType() == VolumeInfo.TYPE_PUBLIC|| vol.getType() == VolumeInfo.TYPE_STUB) {mExternalCategory.addPreference(new StorageVolumePreference(context, vol, 0));}}

packages/apps/Settings/src/com/android/settings/deviceinfo/StorageVolumePreference.java

    public StorageVolumePreference(Context context, VolumeInfo volume, long totalBytes) {super(context);mStorageManager = context.getSystemService(StorageManager.class);mVolume = volume;if (volume.isMountedReadable()) {// TODO: move statfs() to background threadfinal File path = volume.getPath();long freeBytes = 0;long usedBytes = 0;if (volume.getType() == VolumeInfo.TYPE_PRIVATE) {final StorageStatsManager stats =context.getSystemService(StorageStatsManager.class);try {//作为TYPE_PRIVATE,调用StorageStatsManager.getTotalBytes接口获取存储总容量大小totalBytes = stats.getTotalBytes(volume.getFsUuid());//作为TYPE_PRIVATE,调用StorageStatsManager.getFreeBytes接口获取存储可用容量大小freeBytes = stats.getFreeBytes(volume.getFsUuid());usedBytes = totalBytes - freeBytes;} catch (IOException e) {Log.w(TAG, e);}} else {// StorageStatsManager can only query private volumes.// Default to previous storage calculation for public volumes.if (totalBytes <= 0) {/*作为便携式存储,调用File.getTotalSpace接口获取存储总容量大小。注意此处并没有调用FileUtils.roundStorageSize接口进行向上整数对齐,那么为什么这个SD卡被格式化为便携式存储设备后在设置中显示的是"16GB"整数呢,下面会有详细解答*/totalBytes = path.getTotalSpace();}freeBytes = path.getFreeSpace();//作为便携式存储,调用File.getFreeSpace接口获取存储可用容量大小usedBytes = totalBytes - freeBytes;}final String used = Formatter.formatFileSize(context, usedBytes);final String total = Formatter.formatFileSize(context, totalBytes);setSummary(context.getString(R.string.storage_volume_summary, used, total));if (totalBytes > 0) {mUsedPercent = (int) ((usedBytes * 100) / totalBytes);}

frameworks/base/core/java/android/app/usage/StorageStatsManager.java

    private final IStorageStatsManager mService;public @BytesLong long getTotalBytes(@NonNull UUID storageUuid) throws IOException {try {return mService.getTotalBytes(convert(storageUuid), mContext.getOpPackageName());} catch (ParcelableException e) {e.maybeRethrow(IOException.class);throw new RuntimeException(e);} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}public @BytesLong long getFreeBytes(@NonNull UUID storageUuid) throws IOException {try {return mService.getFreeBytes(convert(storageUuid), mContext.getOpPackageName());} catch (ParcelableException e) {e.maybeRethrow(IOException.class);throw new RuntimeException(e);} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}

frameworks/base/core/java/android/app/usage/IStorageStatsManager.aidl

interface IStorageStatsManager {boolean isQuotaSupported(String volumeU

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/456703.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Qt】常见问题

1.存在未解析的标识符 将build文件夹删掉重新编译。 2.左侧项目目录栏无法删除已添加项目 打开目标项目上一级的pro文件&#xff0c;将目标文件名字注释或者删除掉&#xff0c;最后保存&#xff0c;qt就会自动更新&#xff0c;将该项目隐藏掉。 3.在qt creator下添加槽函数…

mmdetection使用自己的voc数据集训练模型实战

一.自己数据集整理 将labelimg格式数据集进行整理 1.1. 更换图片后缀为jpg import os import shutilroot_path/media/ai-developer/imgfileos.listdir(root_path)for img in file:if img.endswith(jpeg) or img.endswith(JPG) or img.endswith(png):img_pathos.path.join(root…

【PTA选择题/基础夯实/期末复习】链表文件操作

2-1 对于一个头指针为head的带头结点的单链表&#xff0c;判定该表为空表的条件是&#xff08;&#xff09;。 A.headNULL B.head→nextNULL C.head→nexthead D.head!NULL 2-2 链表不具有的特点是&#xff08;&#xff09;。 A.可随机访问任一元素 B.插入、删除不需要移…

Express框架介绍—node.js

Express—Node.js 官网传送门(opens new window) 基于 Node.js 平台&#xff0c;快速、开放、极简的 Web 开发框架 Express 是用于快速创建服务器的第三方模块。 Express 初体验 基本使用 安装 Express&#xff1a; npm install express创建服务器&#xff0c;监听客户端请…

PyTorch 2.2 中文官方教程(十九)

使用 RPC 进行分布式管道并行 原文&#xff1a;pytorch.org/tutorials/intermediate/dist_pipeline_parallel_tutorial.html 译者&#xff1a;飞龙 协议&#xff1a;CC BY-NC-SA 4.0 作者&#xff1a;Shen Li 注意 在github中查看并编辑本教程。 先决条件&#xff1a; PyTorc…

Java13常用类3:Date类

4. Date类1&#xff1a;jdk1.8 之前 jdk1.8 之前的API&#xff1a; System类的currentTimeMillis()&#xff1a;获取当前系统时间两个Date类&#xff1a; SimpleDateFormate类&#xff1a;用于格式化、解析Calendar日历类的使用&#xff1a;抽象类 4.1 两个Date类 时间戳&…

《动手学深度学习(PyTorch版)》笔记6.1

注&#xff1a;书中对代码的讲解并不详细&#xff0c;本文对很多细节做了详细注释。另外&#xff0c;书上的源代码是在Jupyter Notebook上运行的&#xff0c;较为分散&#xff0c;本文将代码集中起来&#xff0c;并加以完善&#xff0c;全部用vscode在python 3.9.18下测试通过&…

Python循环语句——for循环的基础语法

一、引言 在Python编程的世界中&#xff0c;for循环无疑是一个强大的工具。它为我们提供了一种简洁、高效的方式来重复执行某段代码&#xff0c;从而实现各种复杂的功能。无论你是初学者还是资深开发者&#xff0c;掌握for循环的用法都是必不可少的。在本文中&#xff0c;我们…

第三百一十回

文章目录 1. 概念介绍2. 实现方法2.1 hintText2.2 labelText2.3 controller 3. 示例代码4. 内容总结 我们在上一章回中介绍了"如何在输入框中处理光标"相关的内容&#xff0c;本章回中将介绍如何添加输入框默认值.闲话休提&#xff0c;让我们一起Talk Flutter吧。 1.…

【C++从0到王者】第四十一站:特殊类的设计

文章目录 一、设计一个类&#xff0c;不能被拷贝1.C98方法2.C11方法 二、设计一个类&#xff0c;只能在堆上创建对象1.析构函数私有化2.构造函数私有化 三、请设计一个类&#xff0c;只能在栈上创建对象四、设计一个类不能被继承1.C98方式2.C11方式 五、设计一个类&#xff0c;…

【JS逆向学习】今日头条

逆向目标 目标网页&#xff1a;https://www.toutiao.com/?wid1707099375036目标接口&#xff1a;https://www.toutiao.com/api/pc/list/feed目标参数&#xff1a;_signature 逆向过程 老规矩先观察网络请求&#xff0c;过滤XHR请求观察加密参数&#xff0c;发现Payload的_s…

c#cad 创建-直线(五)

运行环境 vs2022 c# cad2016 调试成功 一、代码说明 这段代码是用于在AutoCAD中创建一条直线。首先获取当前活动文档和数据库的引用&#xff0c;然后创建一个编辑器对象用于提示用户输入。接下来&#xff0c;在一个事务中获取模型空间的块表记录&#xff0c;并定义直线的长度…