现在的位置: 首页 > 移动开发 > 正文

Android SDCard操作(文件读写,容量计算)

2019年10月10日 移动开发 ⁄ 共 4421字 ⁄ 字号 评论关闭

android.os.Environment

提供访问环境变量

 

java.lang.Object  
  android.os.Environment  

Environment 静态方法:

 

方法 : getDataDirectory ()

返回 : File

解释 : 返回Data的目录


方法 : getDownloadCacheDirectory ()

返回 : File

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


方法 : getExternalStorageDirectory ()

返回 : File

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


方法 : getExternalStoragePublicDirectory (String type)

返回 : 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的目录即可(见源代码)

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

计算SDCard的容量大小

android.os.StatFs

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

 

java.lang.Object  
  android.os.StatFs

 

构造方法:

StatFs (String path)

 

公用方法:

 

方法 : getAvailableBlocks ()

返回 : int

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

 

方法 : getBlockCount ()

返回 : int

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

 

方法 : getBlockSize ()

返回 : int

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

 

方法 : getFreeBlocks ()

返回 : int

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

 

方法 : restat (String path)

返回 : void

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

 

 

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

 

Java代码 复制代码 收藏代码
  1.     public void SDCardSizeTest() {
      
  2.   
  3. // 取得SDCard当前的状态   
  4. String sDcString = android.os.Environment.getExternalStorageState();   
  5.   
  6. if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {   
  7.   
  8.     // 取得sdcard文件路径   
  9.     File pathFile = android.os.Environment   
  10.             .getExternalStorageDirectory();   
  11.   
  12.     android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
      
  13.   
  14.     // 获取SDCard上BLOCK总数   
  15.     long nTotalBlocks = statfs.getBlockCount();   
  16.   
  17.     // 获取SDCard上每个block的SIZE   
  18.     long nBlocSize = statfs.getBlockSize();   
  19.   
  20.     // 获取可供程序使用的Block的数量   
  21.     long nAvailaBlock = statfs.getAvailableBlocks();   
  22.   
  23.     // 获取剩下的所有Block的数量(包括预留的一般程序无法使用的块)   
  24.     long nFreeBlock = statfs.getFreeBlocks();   
  25.   
  26.     // 计算SDCard 总容量大小MB   
  27.     long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
      
  28.   
  29.     // 计算 SDCard 剩余大小MB   
  30.     long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
      
  31. }// end of if   
  32. // end of func  

 

主要:

在判断SDCard的状态为MEDIA_MOUNTED后,仅是说明硬件已经准备好可读可写

如果想叫你的程序也可写的话,还需要在工程的XML文件中加上操作扩展存储设备的权限

 

方法:

在 XML文件中 Permissions 标签中 点击Add 添加 Uses Permission 名字选择 

android.permission.WRITE_EXTERNAL_STORAGE

 

或者直接在xml文件中添加下面一句即可.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

抱歉!评论已关闭.