你的位置:首页 > 操作系统

[操作系统]查看源代码查找获取sd卡剩余容量的代码


 

  • 下载android源码,找到app下的Settings应用源码,导入Settings项目(android项目源码)
  • 查找“可用空间”得到

     <string name="memory_available" msgid="418542433817289474">"可用空间"</string>
  • 查找"memory_available",得到

    <Preference android:key="memory_sd_avail"     android:title="@string/memory_available"  android:summary="00"/>
  • 查找"memorysdavail",得到

    //这个字符串就是sd卡剩余容量formatSize(availableBlocks * blockSize) + readOnly//这两个参数相乘,得到sd卡以字节为单位的剩余容量availableBlocks * blockSize
  • 存储设备会被分为若干个区块,每个区块有固定的大小

  • 区块大小 * 区块数量 等于 存储设备的总大小
  • 示例代码

    File path = Environment.getExternalStorageDirectory();  StatFs stat = new StatFs(path.getPath());  long blockSize;  long totalBlocks;  long availableBlocks;  //获取当前系统版本的等级  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){     blockSize = stat.getBlockSizeLong();     totalBlocks = stat.getBlockCountLong();     availableBlocks = stat.getAvailableBlocksLong();  }  else{    blockSize = stat.getBlockSize();    totalBlocks = stat.getBlockCount();    availableBlocks = stat.getAvailableBlocks();  }  TextView tv = (TextView) findViewById(R.id.tv);  tv.setText(formatSize(availableBlocks * blockSize));}private String formatSize(long size) {  return Formatter.formatFileSize(this, size);}