六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

Android 查看可用存储内存大小

[复制链接]

升级  93.33%

52

主题

52

主题

52

主题

秀才

Rank: 2

积分
190
 楼主| 发表于 2013-1-15 02:30:22 | 显示全部楼层 |阅读模式
下面为查看可用存储内存大小的示例,用于查看和内部和外部存储器的总存储。
 
import java.io.File;     import android.os.Environment;   import android.os.StatFs;     public class MemoryStatus {         static final int ERROR = -1;              static public boolean externalMemoryAvailable() {           return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);       }              static public long getAvailableInternalMemorySize() {           File path = Environment.getDataDirectory();           StatFs stat = new StatFs(path.getPath());           long blockSize = stat.getBlockSize();           long availableBlocks = stat.getAvailableBlocks();           return availableBlocks * blockSize;       }              static public long getTotalInternalMemorySize() {           File path = Environment.getDataDirectory();           StatFs stat = new StatFs(path.getPath());           long blockSize = stat.getBlockSize();           long totalBlocks = stat.getBlockCount();           return totalBlocks * blockSize;       }              static public long getAvailableExternalMemorySize() {           if(externalMemoryAvailable()) {               File path = Environment.getExternalStorageDirectory();               StatFs stat = new StatFs(path.getPath());               long blockSize = stat.getBlockSize();               long availableBlocks = stat.getAvailableBlocks();               return availableBlocks * blockSize;           } else {               return ERROR;           }       }              static public long getTotalExternalMemorySize() {           if(externalMemoryAvailable()) {               File path = Environment.getExternalStorageDirectory();               StatFs stat = new StatFs(path.getPath());               long blockSize = stat.getBlockSize();               long totalBlocks = stat.getBlockCount();               return totalBlocks * blockSize;           } else {               return ERROR;           }       }              static public String formatSize(long size) {           String suffix = null;                  if (size >= 1024) {               suffix = "KiB";               size /= 1024;               if (size >= 1024) {                   suffix = "MiB";                   size /= 1024;               }           }                  StringBuilder resultBuffer = new StringBuilder(Long.toString(size));                  int commaOffset = resultBuffer.length() - 3;           while (commaOffset > 0) {               resultBuffer.insert(commaOffset, ',');               commaOffset -= 3;           }                  if (suffix != null)               resultBuffer.append(suffix);           return resultBuffer.toString();       }   }   
 
 来个更简单的
 
        private void update() {        File path = Environment.getExternalStorageDirectory();        StatFs stat = new StatFs(path.getPath());        long blockSize = stat.getBlockSize();        long totalBlocks = stat.getBlockCount();        long availableBlocks = stat.getAvailableBlocks();        mTotalSize.setText(formatSize(totalBlocks * blockSize));        mUsedSize.setText(formatSize((totalBlocks - availableBlocks) * blockSize));        mAvailableSize.setText(formatSize(availableBlocks * blockSize));}    private String formatSize(long size) {        return Formatter.formatFileSize(this, size);    } 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表