原文连接:http://hi.baidu.com/123330524/blog/item/1831af09c88bb1df62d986be.html



android.os.Environment

提供访问环境变量

java.lang.Object

android.os.Environment

Environment静态方法:

方法 :getDataDirectory()

返回 :File

解释 : 返回Data的目录

方法 :getDownloadCacheDirectory()

返回 :File

解释 : 返回下载缓冲区目录

方法 :getExternalStorageDirectory()

返回 :File

解释 : 返回扩展存储区目录(SDCard)


方法 :getExternalStoragePublicDirectory(Stringtype)

返回 :File

解释 : 返回一个高端的公用的外部存储器目录来摆放某些类型的文件(来自网上)

方法 :getRootDirectory()

返回 :File

解释 : 返回Android的根目录

方法 :getExternalStorageState()

返回 :String

解释 : 返回外部存储设备的当前状态

getExternalStorageState() 返回的状态String类型常量 :

常量 :MEDIA_BAD_REMOVAL

值 : "bad_removal"

解释 : 在没有正确卸载SDCard之前移除了

常量 : MEDIA_CHECKING

值 : "checking"

解释 : 正在磁盘检查

常量 :MEDIA_MOUNTED

值 : "mounted"

解释 : 已经挂载并且拥有可读可写权限

常量 :MEDIA_MOUNTED_READ_ONLY

值 : "mounted_ro"

解释 : 已经挂载,但只拥有可读权限

常量 : MEDIA_NOFS

值 : "nofs"

解释 : 对象空白,或者文件系统不支持

常量 :MEDIA_REMOVED

值 : "removed"

解释 : 已经移除扩展设备

常量 :MEDIA_SHARED

值 : "shared"

解释 : 如果SDCard未挂载,并通过USB大容量存储共享

常量 :MEDIA_UNMOUNTABLE

值 : "unmountable"

解释 : 不可以挂载任何扩展设备

常量 :MEDIA_UNMOUNTED

值 : "unmounted"

解释 : 已经卸载

使用时只需先判断SDCard当前的状态然后取得SdCard的目录即可(见源代码)

  1. //SDcard 操作 //SDcard 操作
  2. ublicvoidSDCardTest() {
  • // 获取扩展SD卡设备状态
  • String sDStateString = android.os.Environment.getExternalStorageState();
  • // 拥有可读可写权限
  • if(sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
  • try{
  • // 获取扩展存储设备的文件目录
  • File SDFile = android.os.Environment
  • .getExternalStorageDirectory();
  • // 打开文件
  • File myFile =newFile(SDFile.getAbsolutePath()
  • + File.separator +"MyFile.txt");
  • // 判断是否存在,不存在则创建
  • if(!myFile.exists()) {
  • myFile.createNewFile();
  • }
  • // 写数据
  • String szOutText ="Hello, World!";
  • FileOutputStream outputStream =newFileOutputStream(myFile);
  • outputStream.write(szOutText.getBytes());
  • outputStream.close();
  • }catch(Exception e) {
  • // TODO: handle exception
  • }// end of try
  • }// end of if(MEDIA_MOUNTED)
  • // 拥有只读权限
  • elseif(sDStateString
  • .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
  • // 获取扩展存储设备的文件目录
  • File SDFile = android.os.Environment.getExternalStorageDirectory();
  • // 创建一个文件
  • File myFile =newFile(SDFile.getAbsolutePath() + File.separator
  • +"MyFile.txt");
  • // 判断文件是否存在
  • if(myFile.exists()) {
  • try{
  • // 读数据
  • FileInputStream inputStream =newFileInputStream(myFile);
  • byte[] buffer =newbyte[1024];
  • inputStream.read(buffer);
  • inputStream.close();
  • }catch(Exception e) {
  • // TODO: handle exception
  • }// end of try
  • }// end of if(myFile)
  • }// end of if(MEDIA_MOUNTED_READ_ONLY)
  • // end of func
        //SDcard 操作  public void SDCardTest() {   // 获取扩展SD卡设备状态   String sDStateString = android.os.Environment.getExternalStorageState();    // 拥有可读可写权限   if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {     try {      // 获取扩展存储设备的文件目录     File SDFile = android.os.Environment       .getExternalStorageDirectory();      // 打开文件     File myFile = new File(SDFile.getAbsolutePath()       + File.separator + "MyFile.txt");      // 判断是否存在,不存在则创建     if (!myFile.exists()) {      myFile.createNewFile();     }      // 写数据     String szOutText = "Hello, World!";     FileOutputStream outputStream = new FileOutputStream(myFile);     outputStream.write(szOutText.getBytes());     outputStream.close();     } catch (Exception e) {     // TODO: handle exception    }// end of try    }// end of if(MEDIA_MOUNTED)   // 拥有只读权限   else if (sDStateString     .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {     // 获取扩展存储设备的文件目录    File SDFile = android.os.Environment.getExternalStorageDirectory();     // 创建一个文件    File myFile = new File(SDFile.getAbsolutePath() + File.separator      + "MyFile.txt");     // 判断文件是否存在    if (myFile.exists()) {     try {       // 读数据      FileInputStream inputStream = new FileInputStream(myFile);      byte[] buffer = new byte[1024];      inputStream.read(buffer);      inputStream.close();      } catch (Exception e) {      // TODO: handle exception     }// end of try    }// end of if(myFile)   }// end of if(MEDIA_MOUNTED_READ_ONLY)  }// end of func

    计算SDCard的容量大小

    1. ublicvoidSDCardTest() {
    2. // 获取扩展SD卡设备状态
    3. String sDStateString = android.os.Environment.getExternalStorageState();
    4. // 拥有可读可写权限
    5. if(sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {
    6. try{
    7. // 获取扩展存储设备的文件目录
    8. File SDFile = android.os.Environment
    9. .getExternalStorageDirectory();
    10. // 打开文件
    11. File myFile =newFile(SDFile.getAbsolutePath()
    12. + File.separator +"MyFile.txt");
    13. // 判断是否存在,不存在则创建
    14. if(!myFile.exists()) {
    15. myFile.createNewFile();
    16. }
    17. // 写数据
    18. String szOutText ="Hello, World!";
    19. FileOutputStream outputStream =newFileOutputStream(myFile);
    20. outputStream.write(szOutText.getBytes());
    21. outputStream.close();
    22. }catch(Exception e) {
    23. // TODO: handle exception
    24. }// end of try
    25. }// end of if(MEDIA_MOUNTED)
    26. // 拥有只读权限
    27. elseif(sDStateString
    28. .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
    29. // 获取扩展存储设备的文件目录
    30. File SDFile = android.os.Environment.getExternalStorageDirectory();
    31. // 创建一个文件
    32. File myFile =newFile(SDFile.getAbsolutePath() + File.separator
    33. +"MyFile.txt");
    34. // 判断文件是否存在
    35. if(myFile.exists()) {
    36. try{
    37. // 读数据
    38. FileInputStream inputStream =newFileInputStream(myFile);
    39. byte[] buffer =newbyte[1024];
    40. inputStream.read(buffer);
    41. inputStream.close();
    42. }catch(Exception e) {
    43. // TODO: handle exception
    44. }// end of try
    45. }// end of if(myFile)
    46. }// end of if(MEDIA_MOUNTED_READ_ONLY)
    47. // end of func

        //SDcard 操作  public void SDCardTest() {
       // 获取扩展SD卡设备状态 
     String sDStateString = android.os.Environment.getExternalStorageState();    // 拥有可读可写权限   if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {     try {      // 获取扩展存储设备的文件目录     File SDFile = android.os.Environment       .getExternalStorageDirectory();      // 打开文件     File myFile = new File(SDFile.getAbsolutePath()       + File.separator + "MyFile.txt");      // 判断是否存在,不存在则创建     if (!myFile.exists()) {      myFile.createNewFile();     }      // 写数据     String szOutText = "Hello, World!";     FileOutputStream outputStream = new FileOutputStream(myFile);     outputStream.write(szOutText.getBytes());     outputStream.close();     } catch (Exception e) {     // TODO: handle exception    }// end of try    }// end of if(MEDIA_MOUNTED)   // 拥有只读权限   else if (sDStateString     .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {     // 获取扩展存储设备的文件目录    File SDFile = android.os.Environment.getExternalStorageDirectory();     // 创建一个文件    File myFile = new File(SDFile.getAbsolutePath() + File.separator      + "MyFile.txt");     // 判断文件是否存在    if (myFile.exists()) {     try {       // 读数据      FileInputStream inputStream = new FileInputStream(myFile);      byte[] buffer = new byte[1024];      inputStream.read(buffer);      inputStream.close();      } catch (Exception e) {      // TODO: handle exception     }// end of try    }// end of if(myFile)   }// end of if(MEDIA_MOUNTED_READ_ONLY)  }// end of func


    计算SDCard的容量大小

    android.os.StatFs

    一个模拟linux的df命令的一个类,获得SD卡和手机内存的使用情况

    java.lang.Object

    android.os.StatFs

    构造方法:

    StatFs(Stringpath)

    公用方法:

    方法 :getAvailableBlocks()

    返回 : int

    解释 :返回文件系统上剩下的可供程序使用的块

    方法 :getBlockCount()

    返回 : int

    解释 : 返回文件系统上总共的块

    方法 :getBlockSize()

    返回 : int

    解释 : 返回文件系统 一个块的大小单位byte

    方法 :getFreeBlocks()

    返回 : int

    解释 : 返回文件系统上剩余的所有块 包括预留的一般程序无法访问的

    方法 :restat(Stringpath)

    返回 : void

    解释 : 执行一个由该对象所引用的文件系统雷斯塔特.(Google翻译)


    想计算SDCard大小和使用情况时, 只需要得到SD卡总共拥有的Block数或是剩余没用的Block数,再乘以每个Block的大小就是相应的容量大小了单位byte.(见代码)

    1. publicvoidSDCardSizeTest() {
    2. // 取得SDCard当前的状态
    3. String sDcString = android.os.Environment.getExternalStorageState();
    4. if(sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
    5. // 取得sdcard文件路径
    6. File pathFile = android.os.Environment
    7. .getExternalStorageDirectory();
    8. android.os.StatFs statfs =newandroid.os.StatFs(pathFile.getPath());
    9. // 获取SDCard上BLOCK总数
    10. longnTotalBlocks = statfs.getBlockCount();
    11. // 获取SDCard上每个block的SIZE
    12. longnBlocSize = statfs.getBlockSize();
    13. // 获取可供程序使用的Block的数量
    14. longnAvailaBlock = statfs.getAvailableBlocks();
    15. // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)
    16. longnFreeBlock = statfs.getFreeBlocks();
    17. // 计算SDCard 总容量大小MB
    18. longnSDTotalSize = nTotalBlocks * nBlocSize /1024/1024;
    19. // 计算 SDCard 剩余大小MB
    20. longnSDFreeSize = nAvailaBlock * nBlocSize /1024/1024;
    21. }// end of if
    22. // end of func

    更多相关文章

    1. Android 文件操作总结
    2. 调用android的getColor()方法出现 java.lang.NoSuchMethodError:
    3. Android自适应不同版本的程序退出方法
    4. Android Studio报错:Unsupported method: AndroidProject.getPlug
    5. android中的ProgressBar的使用方法
    6. Some Android licenses not accepted. To resolve this, run: fl
    7. android Button控件的四种不同方法
    8. android去掉标题和状态栏的方法

    随机推荐

    1. 自定义控件 - 圆形缓冲进度条
    2. Android中Parcelable接口用法
    3. Android(安卓)速度检测demo
    4. android > layout > SeekBar (拉动条)
    5. Programmatically Injecting Events on A
    6. Programmatically Injecting Events on A
    7. android 界面常亮设置
    8. android获取网络类型
    9. Android版本和API level对应关系
    10. 【Android】数据存储之Network