获取 Android 上的所有可用空间 [英] Getting all the total and available space on Android

查看:53
本文介绍了获取 Android 上的所有可用空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,在 Android 上,有:

To my knowledge, on Android, there is:

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

如何通过检查它们是否存在来获取每个设备的总数和可用空间(有些手机没有内置 SD 卡)

How do I get the total and available for each with a check that they exist (some phones do not have an internal SD Card)

谢谢

推荐答案

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

Here is how you get internal storage sizes:

 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 卡大小)的方法:

Here is how you get external storage sizes (SD card size):

 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;

<小时>

简短说明

空闲块:

区块总数在文件系统上免费,包括保留块(不是可用于普通应用程序).

The total number of blocks that are free on the file system, including reserved blocks (that are not available to normal applications).

可用块:

空闲块数文件系统,可用于应用程序.

The number of blocks that are free on the file system and available to applications.

<小时>

这里是如何检测是否安装了SD卡:


Here is how to detect whether SD card is mounted:

 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
 }

这篇关于获取 Android 上的所有可用空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆