zoukankan      html  css  js  c++  java
  • Android 关于SD卡、机身内存以及分辨率的转换的工具类

      翻了代码,记录一下Android 关于SD卡、机身可用空间,以及分辨率的转换的工具类。

      代码很简单也很使用,主要用于下载或者生成大文件时检查可用空间。   勿喷....

      

     1 import java.io.File;
     2 
     3 import android.content.Context;
     4 import android.os.Environment;
     5 import android.os.StatFs;
     6 
     7 public class CommonUtil {
     8 
     9     /**
    10      * 检测sdcard是否可用
    11      * 
    12      * @return true为可用,否则为不可用
    13      */
    14     public static boolean sdCardIsAvailable() {
    15         String status = Environment.getExternalStorageState();
    16         if (!status.equals(Environment.MEDIA_MOUNTED))
    17             return false;
    18         return true;
    19     }
    20 
    21     /**
    22      * 检测内存上的剩余空间够不够用,以及是否可用
    23      */
    24     public static boolean enoughSpaceOnSdCard(long downloadSize) {
    25         String status = Environment.getExternalStorageState();
    26         if (!status.equals(Environment.MEDIA_MOUNTED))
    27             return false;
    28         return (downloadSize < getRealSizeOnSdcard());
    29     }
    30 
    31     /**
    32      * 获得SD卡上的剩余空间
    33      */
    34     public static long getRealSizeOnSdcard() {
    35         File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    36         StatFs stat = new StatFs(path.getPath());
    37         long blockSize = stat.getBlockSize();
    38         long availableBlocks = stat.getAvailableBlocks();
    39         return availableBlocks * blockSize;
    40     }
    41 
    42     /**
    43      * 检查机身内存是否够用
    44      */
    45     public static boolean enoughSpaceOnPhone(long downloadSize) {
    46         return getRealSizeOnPhone() > downloadSize;
    47     }
    48 
    49     /**
    50      * 获得机身剩余内存
    51      */
    52     public static long getRealSizeOnPhone() {
    53         File path = Environment.getDataDirectory();
    54         StatFs stat = new StatFs(path.getPath());
    55         long blockSize = stat.getBlockSize();
    56         long availableBlocks = stat.getAvailableBlocks();
    57         long realSize = blockSize * availableBlocks;
    58         return realSize;
    59     }
    60     
    61     /**
    62      * 根据手机分辨率从dp转成px
    63      */
    64     public static  int dip2px(Context context, float dpValue) {  
    65         final float scale = context.getResources().getDisplayMetrics().density;  
    66         return (int) (dpValue * scale + 0.5f);  
    67     }  
    68       
    69     /** 
    70      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
    71      */  
    72     public static  int px2dip(Context context, float pxValue) {  
    73         final float scale = context.getResources().getDisplayMetrics().density;  
    74         return (int) (pxValue / scale + 0.5f)-15;  
    75     }  
    76 
    77     
    78 }

      主要就是 StatFs类的应用, Android.os下的StatFs类主要用来获取文件系统的状态,能够获取sd卡的大小和剩余空间,获取系统内部空间也就是/system的大小和剩余空间等等。

  • 相关阅读:
    GSM Arena 魅族mx四核评测个人翻译
    Oracle Exists用法|转|
    NC公有协同的实现原理|同13的QQ||更新总部往来协同|
    NC客商bd_custbank不可修改账号、名称但可修改默认银行并更新分子公司trigger
    试玩了plsql中test窗口declare声明变量|lpad函数||plsql sql command test window区别|
    使用windows live writer测试
    用友写insert on bd_custbank 触发器和自动更新单位名称2in1
    oracle触发器select into和cursor用法的区别
    |转|oracle中prior的用法,connect by prior,树形目录
    客商增加自动增加银行账户|搞定!||更新使用游标course写法|
  • 原文地址:https://www.cnblogs.com/mauiie/p/3664054.html
Copyright © 2011-2022 走看看