获取data目录剩余存储空间大小,并格式化字符串

//格式化
public static String formatM(long length){
		 String  show = "";
		 int sub_index = 0;
	if(length>=1073741824){
            sub_index = (String.valueOf((float)length/1073741824)).indexOf(".");
            show = ((float)length/1073741824+"000").substring(0,sub_index+3)+"GB";
          }else if(length>=1048576){
            sub_index = (String.valueOf((float)length/1048576)).indexOf(".");
            show =((float)length/1048576+"000").substring(0,sub_index+3)+"MB";
          }else if(length>=1024){
            sub_index = (String.valueOf((float)length/1024)).indexOf(".");
            show = ((float)length/1024+"000").substring(0,sub_index+3)+"KB";
          }else if(length<1024){
            show = String.valueOf(length)+"B";
          }
		
		return show;
	}
	

        String path = "/data";
        StatFs fileStats = new StatFs(path); 
        fileStats.restat(path);
 long restM = fileStats.getAvailableBlocks() * fileStats.getBlockSize(); //获取剩余空间 

formatM(restM); //格式化如返回: 2MB 或200KB 等类似的字符串

我的邮箱:[email protected],欢迎交流。


 
  
 
  
 
 

你可能感兴趣的:(android,java)