Android13 SD卡格式化问题分析

问题描述

Android13 上进行SD卡格式化,格式化后显示的SD卡容量为0,退出重新进入,显示正常。

源码分析

首先在Settings->Storage页面,切换到SD card,页面将显示SD card的存储信息

切换到sdcard.png

代码位于packages/apps/Settings/src/com/android/settings/deviceinfo/StorageDashboardFragment.java

 

java

复制代码

@Override public void onResume() { super.onResume(); if (mIsLoadedFromCache) { mIsLoadedFromCache = false; } else { mStorageEntries.clear(); mStorageEntries.addAll( StorageUtils.getAllStorageEntries(getContext(), mStorageManager)); Log.d("jasonwan", "---refreshUi-09---"); //刷新UI refreshUi(); } mStorageManager.registerListener(mStorageEventListener); } private void refreshUi() { mStorageSelectionController.setStorageEntries(mStorageEntries); mStorageSelectionController.setSelectedStorageEntry(mSelectedStorageEntry); //设置已选择的存储项 mStorageUsageProgressBarController.setSelectedStorageEntry(mSelectedStorageEntry); mOptionMenuController.setSelectedStorageEntry(mSelectedStorageEntry); getActivity().invalidateOptionsMenu(); // To prevent flicker, hides secondary users preference. // onReceivedSizes will set it visible for private storage. setSecondaryUsersVisible(false); if (!mSelectedStorageEntry.isMounted()) { // Set null volume to hide category stats. mPreferenceController.setVolume(null); return; } Log.d("jasonwan", " mStorageCacheHelper.hasCachedSizeInfo()="+mStorageCacheHelper.hasCachedSizeInfo()); Log.d("jasonwan", " mSelectedStorageEntry.isPrivate()="+mSelectedStorageEntry.isPrivate()); //sdcard属于public volume,因此这里的isPrivate()为false if (mStorageCacheHelper.hasCachedSizeInfo() && mSelectedStorageEntry.isPrivate()) { StorageCacheHelper.StorageCache cachedData = mStorageCacheHelper.retrieveCachedSize(); mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo()); mPreferenceController.setUsedSize(cachedData.totalUsedSize); mPreferenceController.setTotalSize(cachedData.totalSize); Log.d("jasonwan", " totalUsedSize="+cachedData.totalUsedSize+", totalSize="+cachedData.totalSize); } if (mSelectedStorageEntry.isPrivate()) { mStorageInfo = null; mAppsResult = null; // Hide the loading spinner if there is cached data. if (mStorageCacheHelper.hasCachedSizeInfo()) { //TODO(b/220259287): apply cache mechanism to secondary user mPreferenceController.onLoadFinished(mAppsResult, mUserId); } else { maybeSetLoading(isQuotaSupported()); // To prevent flicker, sets null volume to hide category preferences. // onReceivedSizes will setVolume with the volume of selected storage. mPreferenceController.setVolume(null); } // Stats data is only available on private volumes. getLoaderManager().restartLoader(STORAGE_JOB_ID, Bundle.EMPTY, this); getLoaderManager() .restartLoader(VOLUME_SIZE_JOB_ID, Bundle.EMPTY, new VolumeSizeCallbacks()); getLoaderManager().restartLoader(ICON_JOB_ID, Bundle.EMPTY, new IconLoaderCallbacks()); } else { mPreferenceController.setVolume(mSelectedStorageEntry.getVolumeInfo()); } }

refreshUi方法中,会进行容量大小的显示,sdcard因为是public volume,所以isPrivate()方法为false,sdcard的容量大小计算及显示在mStorageUsageProgressBarController.setSelectedStorageEntry(mSelectedStorageEntry)

 

java

复制代码

/** Set Sto

Android 4.4.4 系统,你可以使用以下代码来格式化 SD 卡: ```java import android.os.Environment; import android.os.storage.StorageManager; import android.os.storage.StorageVolume; import java.io.File; import java.lang.reflect.Method; public class SDCardUtils { public static void formatSDCard() { File sdCard = Environment.getExternalStorageDirectory(); StorageManager storageManager = (StorageManager) getSystemService(Context.STORAGE_SERVICE); Class<?> storageVolumeClazz = null; try { storageVolumeClazz = Class.forName("android.os.storage.StorageVolume"); Method getVolumeList = storageManager.getClass().getMethod("getVolumeList"); Method getUuid = storageVolumeClazz.getMethod("getUuid"); Method getPath = storageVolumeClazz.getMethod("getPath"); Method format = storageManager.getClass().getMethod("format", String.class); Object[] storageVolumes = (Object[]) getVolumeList.invoke(storageManager); for (Object storageVolume : storageVolumes) { String path = (String) getPath.invoke(storageVolume); if (path.equals(sdCard.getAbsolutePath())) { String uuid = (String) getUuid.invoke(storageVolume); format.invoke(storageManager, uuid); break; } } } catch (Exception e) { e.printStackTrace(); } } } ``` 请注意,这段代码使用了反射来调用隐藏的 API。在 Android 4.4.4 ,这是一种常见的处理方式。然而,从 Android 5.0(API 级别 21)开始,该方法已被标记为不推荐使用,并且在 Android 10(API 级别 29)已被移除。因此,在较新的 Android 版本,你可能需要使用其他方法来格式化 SD 卡。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值