zoukankan      html  css  js  c++  java
  • Android 判断SD卡是否存在及容量查询

    Android 判断SD卡是否存在及容量查询的简单方法如下:
    首先要在AndroidManifest.xml中增加SD卡访问权限

    <!-- 在SDCard中创建与删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>&nbsp;

    SD卡是否存在

     private boolean ExistSDCard() {
          if (android.os.Environment.getExternalStorageState().equals(
            android.os.Environment.MEDIA_MOUNTED)) {
           return true;
          } else
           return false;
         }

    SD卡剩余空间

     public long getSDFreeSize(){
          //取得SD卡文件路径
          File path = Environment.getExternalStorageDirectory(); 
          StatFs sf = new StatFs(path.getPath()); 
          //获取单个数据块的大小(Byte)
          long blockSize = sf.getBlockSize(); 
          //空闲的数据块的数量
          long freeBlocks = sf.getAvailableBlocks();
          //返回SD卡空闲大小
          //return freeBlocks * blockSize;  //单位Byte
          //return (freeBlocks * blockSize)/1024;   //单位KB
          return (freeBlocks * blockSize)/1024 /1024; //单位MB
        }    

    SD卡总容量

     public long getSDAllSize(){
          //取得SD卡文件路径
          File path = Environment.getExternalStorageDirectory(); 
          StatFs sf = new StatFs(path.getPath()); 
          //获取单个数据块的大小(Byte)
          long blockSize = sf.getBlockSize(); 
          //获取所有数据块数
          long allBlocks = sf.getBlockCount();
          //返回SD卡大小
          //return allBlocks * blockSize; //单位Byte
          //return (allBlocks * blockSize)/1024; //单位KB
          return (allBlocks * blockSize)/1024/1024; //单位MB
        }    
  • 相关阅读:
    赋值运算符重载
    拷贝构造函数
    sizeof与strlen
    C++函数参数为引用或指针
    Largest Rectangle in Histogram
    二极管作用
    yzoj P1948 取数字问题
    NOIP 2016 愤怒的小鸟 题解
    NOIP 2016 蚯蚓 题解
    NOIP 2016 组合数问题 题解
  • 原文地址:https://www.cnblogs.com/sishuiliuyun/p/2876574.html
Copyright © 2011-2022 走看看