1 在Setting应用中,storage 显示total and free code:

finalFilepath=VolumeInfo.getPath();finallongfreeBytes=path.getFreeSpace();finallongtotalBytes=path.getTotalSpace();finallongusedBytes=totalBytes-freeBytes;finalStringused=Formatter.formatFileSize(context,usedBytes);finalStringtotal=Formatter.formatFileSize(context,totalBytes);

2 VolumeInfo.java是hidden的,只能通过反射得到。代码如下:

try{StorageManagersManager=(StorageManager)context.getSystemService(Context.STORAGE_SERVICE);Classclass_StorageManager=StorageManager.class;Methodmethod_getVolumes=class_StorageManager.getMethod("getVolumes");List<Object>volumes=(List<Object>)method_getVolumes.invoke(sManager);mClassType=Class.forName("android.os.storage.VolumeInfo");for(ObjectvolumeInfo:volumes){mGetMethod=mClassType.getDeclaredMethod("getPath");Filepath=(File)mGetMethod.invoke(volumeInfo);finallongfreeBytes=path.getFreeSpace();finallongtotalBytes=path.getTotalSpace();finallongusedBytes=totalBytes-freeBytes;Log.i("cyy1","freeBytes="+Formatter.formatFileSize(context,freeBytes));Log.i("cyy1","usedBytes="+Formatter.formatFileSize(context,usedBytes));mGetMethod1=mClassType.getDeclaredMethod("getId");Stringid=(String)mGetMethod1.invoke(volumeInfo);Log.i("cyy1","id="+id);}}catch(Exceptione){Log.i("cyy1",e.getMessage());e.printStackTrace();}

得到数据:08-1817:28:31.08732148-32148/I/cyy1:freeBytes=190MB08-1817:28:31.08832148-32148/I/cyy1:usedBytes=22.68GB08-1817:28:31.08832148-32148/I/cyy1:id=private08-1817:28:31.08832148-32148/I/cyy1:freeBytes=9.90MB08-1817:28:31.08832148-32148I/cyy1:usedBytes=22.84GB08-1817:28:31.08832148-32148/I/cyy1:id=emulated

其中emulated 是externalstroage的数据。而private是data的数据3 可以通过以下code验证:Filepath=Environment.getExternalStorageDirectory();StatFsstat=newStatFs(path.getPath());longblockSize=stat.getBlockSizeLong();longblockCount=stat.getBlockCountLong();longavailableBlocks=stat.getAvailableBlocksLong();longavaibByte=stat.getAvailableBytes();Log.i("cyy","avaibByte="+Formatter.formatFileSize(context,avaibByte));longfree=stat.getFreeBytes();Log.i("cyy","free="+Formatter.formatFileSize(context,free));StringtotalSize=Formatter.formatFileSize(context,blockCount*blockSize);StringavailableSize=Formatter.formatFileSize(context,availableBlocks*blockSize);Environment.getExternalStorageDirectory() 跟emulated数据一致;Environment.getDataDirectory()和private数据一致。