12

据我所知,在 Android 上,有:

  • 应用程序和缓存的内部存储器
  • 某些手机​​上的内部 SD 卡(不可移动)
  • 用于音乐和照片的外部 SD 卡(可拆卸)

如何通过检查它们是否存在来获得每个人的总数和可用(某些手机没有内部 SD 卡)

谢谢

4

5 回答 5

33

以下是获取内部存储大小的方法:

 StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

以下是获取外部存储大小(SD 卡大小)的方法:

 StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
 long blockSize = statFs.getBlockSize();
 long totalSize = statFs.getBlockCount()*blockSize;
 long availableSize = statFs.getAvailableBlocks()*blockSize;
 long freeSize = statFs.getFreeBlocks()*blockSize;

简短说明

空闲块:

文件系统上空闲的块总数,包括保留块(对普通应用程序不可用)。

可用块:

文件系统上空闲且可供应用程序使用的块数。


下面是检测SD卡是否挂载的方法:

 String state = Environment.getExternalStorageState();
 if (Environment.MEDIA_MOUNTED.equals(state)) 
 {
   // We can read and write the media    
 } 
 else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) 
 {
    // We can only read the media     
 } 
 else 
 {
    // No external media
 }
于 2011-01-25T22:56:53.283 回答
3
/*************************************************************************************************
Returns size in MegaBytes.

If you need calculate external memory, change this: 
    StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
to this: 
    StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath());        
**************************************************************************************************/
    public int TotalMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        return Total;
    }

    public int FreeMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
        int Free  = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        return Free;
    }

    public int BusyMemory()
    {
        StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());   
        int Total = (statFs.getBlockCount() * statFs.getBlockSize()) / 1048576;
        int Free  = (statFs.getAvailableBlocks() * statFs.getBlockSize()) / 1048576;
        int Busy  = Total - Free;
        return Busy;
    }
于 2012-01-11T20:23:42.720 回答
3

对于内部存储器 Environment.getRootDirectory().getAbsolutePath() 不是正确的方法。

也在大多数设备上

Environment.getDataDirectory() 有效,但又不一致。

对于外部目录方法是正确的。区分内部 mnt/sdcard 使用

Environment.isExternalStorageRemovable()。

但我仍然想知道如何获得确切的内部存储器。与开源不一致和许多版本一样。

于 2012-12-18T23:23:17.090 回答
0

StatFs - 检索有关文件系统空间的整体信息。这是 Unix statvfs() 的包装器。

从 API 级别 18 开始使用

long getTotalBytes - The total number of bytes supported by the file system. 

对于较旧的 API 使用

int getBlockCount ()
int getBlockSize ()

StatFs stat = new StatFs(**path**);
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();

在这里,我们需要为路径变量提供正确的字符串值。您需要使用这样的路径字符串值:“/mnt/external_sd/”“/mnt/extSdCard/”

您可以检索所有/mnt/设备的列表并使用其中一个值。

File mntDir = new File("/mnt/");
if(mntDir.isDirectory()){ 
String[] dirList = mntDir.list();...}

或类似的东西

var file=new Java.IO.File("storage/");
var listOfStorages=file.ListFiles();

或者

String[] externals = System.getenv("SECONDARY_STORAGE").split(":");
于 2018-04-01T23:14:22.047 回答
0

在大多数手机上,总块数是用户可用的块数(即更少的系统存储等)。我还没有找到一种方法可以产生手机上的总存储量。

于 2017-10-23T15:44:25.327 回答