• 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"/>   

    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  
       }   




  • 相关阅读:
    【Office Web Apps】在 SharePoint 中使用 Office Web Apps
    css轮廓
    css定位
    css盒子模型 css3盒子相关样式
    css常用操作
    css3选择器
    强制换行和禁止换行
    text-transform 字母的大小写
    css hack
    JavaScript基本语法
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4020623.html
Copyright © 2020-2023  润新知