首页 > 代码库 > 获取手机剩余空间工具类
获取手机剩余空间工具类
1 import java.io.File; 2 3 import com.iss.utils.LogUtil; 4 5 import android.os.Environment; 6 import android.os.StatFs; 7 8 /** 9 * 类名 10 * @author 王洪贺<br/>11 * 获取手机剩余的存储空间工具类12 * 创建日期 2014年8月1日13 */14 15 public class StorageUtil {16 17 /**18 * 获取sd卡剩余存储空间19 * @return 返回kb字节为单位的long整形数值20 */21 public static long readSDCard() {22 String state = Environment.getExternalStorageState();23 if (Environment.MEDIA_MOUNTED.equals(state)) {24 File sdcardDir = Environment.getExternalStorageDirectory();25 StatFs sf = new StatFs(sdcardDir.getPath());26 long blockSize = sf.getBlockSize();27 long blockCount = sf.getBlockCount();28 long availCount = sf.getAvailableBlocks();29 /* LogUtil.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize30 * blockCount / 1024 + "KB");31 LogUtil.d("", "可用的block数目::" + availCount + ",剩余空间:" + availCount * blockSize / 102432 + "KB");*/33 return availCount * blockSize / 1024;34 } else {35 return 0;36 }37 }38 39 /**40 * 获取sd卡剩余存储空间41 * @return 返回M字节为单位的long整形数值42 */43 public static long readSDCardM() {44 return readSDCard() / 1024;45 }46 47 /**48 * 获取系统剩余存储空间49 * @return 返回以KB为单位的long整形数值50 */51 public static long readSystem() {52 File root = Environment.getRootDirectory();53 StatFs sf = new StatFs(root.getPath());54 long blockSize = sf.getBlockSize();55 long blockCount = sf.getBlockCount();56 long availCount = sf.getAvailableBlocks();57 /*LogUtil.d("", "block大小:" + blockSize + ",block数目:" + blockCount + ",总大小:" + blockSize58 * blockCount / 1024 + "KB");59 LogUtil.d("", "可用的block数目::" + availCount + ",可用大小:" + availCount * blockSize / 1024 + "KB");*/60 return availCount * blockSize / 1024;61 }62 63 /**64 * 获取系统剩余存储空间65 * @return 返回以M为单位的long整形数值66 */67 public static long readSystemM() {68 return readSystem() / 1024;69 }70 71 /**72 * 判断剩余空间是否大于指定空间73 * @param sizeMb 以MB为单位的指定大小74 * @return 返回值true为有相应空间,false为小于对应空间75 */76 public static boolean isAvailableSpace(int sizeMb) {77 boolean hasSpace = false;78 long availableSpare = readSDCardM();79 if (availableSpare > sizeMb) {80 hasSpace = true;81 }82 return hasSpace;83 }84 85 /**86 * 判断剩余空间是否大于指定空间87 * @return 返回值true为有100M,false为小于100M88 */89 public static boolean isAvailableSpace() {90 return isAvailableSpace(100);91 }92 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。