存储空间大小获取异常?难道没人知道?

fisan 发布于 2015/10/30 09:47
阅读 147
收藏 0
从网上找的:

        public static int getSD(){                                              //SD卡大小
                long blockSize=0,availCount=0;
                File sdcardDir = Environment.getExternalStorageDirectory();
                StatFs sf = new StatFs(sdcardDir.getPath());
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                           blockSize = sf.getBlockSizeLong();
                           availCount = sf.getAvailableBlocksLong();                        
                }else{
                   blockSize = sf.getBlockSize();
                   availCount = sf.getAvailableBlocks();

                }

             int count=(int)(availCount*blockSize)/(1024*1024);

                return count;
        }


        public static int getSYS(){                                           //内置大小
                long blockSize=0,availCount=0;
                File sys=Environment.getDataDirectory();
                StatFs sf=new StatFs(sys.getPath());
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2){
                           blockSize = sf.getBlockSizeLong();
                           availCount = sf.getAvailableBlocksLong();                        
                }else{
                   blockSize = sf.getBlockSize();
                   availCount = sf.getAvailableBlocks();

                }

              int count=(int)(availCount*blockSize)/(1024*1024);

                return count;
        }




对于原生态的系统,检测正常

像锤子这种版本系统的,怎么显示不正常,看了一下为负数,还是相同的数
加载中
0
kevend
kevend
package com.chat.assistant.utils;


import java.io.File;


import android.os.Environment;
import android.os.StatFs;


/**
 * SD卡相关的辅助类
 * 
 * @author zhy
 * 
 */
public class SDCardUtils
{
private SDCardUtils()
{
/* cannot be instantiated */
throw new UnsupportedOperationException("cannot be instantiated");
}


/**
* 判断SDCard是否可用

* @return
*/
public static boolean isSDCardEnable()
{
return Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED);


}


/**
* 获取SD卡路径

* @return
*/
public static String getSDCardPath()
{
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}

/**
* 文件保存路径
* @param filePath
* @return
*/
public static String getSavePath(String filePath){
return getSDCardPath() + filePath;
}


/**
* 从httpurl路径中截取文件的文件名
* @param url
* @return
*/
public static String getFileName(String url){
return url.substring(url.lastIndexOf("/") + 1);
}

/**
* 获取SD卡的剩余容量 单位byte

* @return
*/
public static long getSDCardAllSize()
{
if (isSDCardEnable())
{
StatFs stat = new StatFs(getSDCardPath());
// 获取空闲的数据块的数量
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
// 获取单个数据块的大小(byte)
long freeBlocks = stat.getAvailableBlocks();
return freeBlocks * availableBlocks;
}
return 0;
}


/**
* 获取指定路径所在空间的剩余可用容量字节数,单位byte

* @param filePath
* @return 容量字节 SDCard可用空间,内部存储可用空间
*/
public static long getFreeBytes(String filePath)
{
// 如果是sd卡的下的路径,则获取sd卡可用容量
if (filePath.startsWith(getSDCardPath()))
{
filePath = getSDCardPath();
} else
{// 如果是内部存储的路径,则获取内存存储的可用容量
filePath = Environment.getDataDirectory().getAbsolutePath();
}
StatFs stat = new StatFs(filePath);
long availableBlocks = (long) stat.getAvailableBlocks() - 4;
return stat.getBlockSize() * availableBlocks;
}


/**
* 获取系统存储路径

* @return
*/
public static String getRootDirectoryPath()
{
return Environment.getRootDirectory().getAbsolutePath();
}


}

返回顶部
顶部