Chinaunix首页 | 论坛 | 博客
  • 博客访问: 574271
  • 博文数量: 192
  • 博客积分: 3780
  • 博客等级: 中校
  • 技术积分: 1487
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-26 10:11
文章存档

2012年(6)

2011年(160)

2010年(26)

分类: 嵌入式

2010-09-02 11:28:45

Android获取SdCard路径及剩余容量
具体代码如下所示:
 
 

/**
     * 获取SdCard路径
     */

    public static String getExternalStoragePath() {

              // 获取SdCard状态

              String state = android.os.Environment.getExternalStorageState();

              // 判断SdCard是否存在并且是可用的

              if (android.os.Environment.MEDIA_MOUNTED.equals(state)) {
                       if (android.os.Environment.getExternalStorageDirectory().canWrite()) {
                                return android.os.Environment.getExternalStorageDirectory().getPath();
                       }
              }
              return null;
    }
    
    /**
     * 获取存储卡的剩余容量,单位为字节
     * @param filePath
     * @return availableSpare
     */


    public static long getAvailableStore(String filePath) {

              // 取得sdcard文件路径

              StatFs statFs = new StatFs(filePath);

              // 获取block的SIZE

              long blocSize = statFs.getBlockSize();

              // 获取BLOCK数量

// long totalBlocks = statFs.getBlockCount();


              // 可使用的Block的数量

              long availaBlock = statFs.getAvailableBlocks();
              
// long total = totalBlocks * blocSize;


              long availableSpare = availaBlock * blocSize;
              
              return availableSpare;

    }


参考网址:

   

阅读(1623) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~