• 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 (Environment.getExternalStorageState().equals(
                    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
        }
  • 相关阅读:
    更好的抽屉效果(ios)
    系统拍照动画
    UITabBarController详解
    touch事件分发
    iOS UWebView详解
    iOS 监听声音按键
    webservice偶尔报黄页,解决方案
    FastReport脚本把数据绑定到文本控件上
    [转]js版的md5()
    JQuery中$.ajax()方法参数详解
  • 原文地址:https://www.cnblogs.com/jxyZ/p/4028526.html
Copyright © 2020-2023  润新知