Hello Android - Android SDCard操作(文件讀寫,容量計算)

原文連接: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 (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的目錄即可(見源代碼)

  1. //SDcard 操作      //SDcard 操作  
  2. ublic 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 操作  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. ublic void SDCardTest() {  
  2. // 獲取擴展SD卡設備狀態  
  3. String sDStateString = android.os.Environment.getExternalStorageState();  
  4.   
  5. // 擁有可讀可寫權限  
  6. if (sDStateString.equals(android.os.Environment.MEDIA_MOUNTED)) {  
  7.   
  8.     try {  
  9.   
  10.         // 獲取擴展存儲設備的文件目錄  
  11.          File SDFile = android.os.Environment  
  12.                  .getExternalStorageDirectory();  
  13.   
  14.         // 打開文件  
  15.          File myFile = new File(SDFile.getAbsolutePath()  
  16.                  + File.separator + "MyFile.txt");  
  17.   
  18.         // 判斷是否存在,不存在則創建  
  19.         if (!myFile.exists()) {  
  20.              myFile.createNewFile();  
  21.          }  
  22.   
  23.         // 寫數據  
  24.          String szOutText = "Hello, World!";  
  25.          FileOutputStream outputStream = new FileOutputStream(myFile);  
  26.          outputStream.write(szOutText.getBytes());  
  27.          outputStream.close();  
  28.   
  29.      } catch (Exception e) {  
  30.         // TODO: handle exception  
  31.      }// end of try  
  32.   
  33. }// end of if(MEDIA_MOUNTED)  
  34. // 擁有隻讀權限  
  35. else if (sDStateString  
  36.          .endsWith(android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {  
  37.   
  38.     // 獲取擴展存儲設備的文件目錄  
  39.      File SDFile = android.os.Environment.getExternalStorageDirectory();  
  40.   
  41.     // 創建一個文件  
  42.      File myFile = new File(SDFile.getAbsolutePath() + File.separator  
  43.              + "MyFile.txt");  
  44.   
  45.     // 判斷文件是否存在  
  46.     if (myFile.exists()) {  
  47.         try {  
  48.   
  49.             // 讀數據  
  50.              FileInputStream inputStream = new FileInputStream(myFile);  
  51.             byte[] buffer = new byte[1024];  
  52.              inputStream.read(buffer);  
  53.              inputStream.close();  
  54.   
  55.          } catch (Exception e) {  
  56.             // TODO: handle exception  
  57.          }// end of try  
  58.      }// end of if(myFile)  
  59. }// end of if(MEDIA_MOUNTED_READ_ONLY)  
  60. // 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 (String path)

公用方法:

方法 : getAvailableBlocks ()

返回 : int

解釋 :返回文件系統上剩下的可供程序使用的塊

方法 : getBlockCount ()

返回 : int

解釋 : 返回文件系統上總共的塊

方法 : getBlockSize ()

返回 : int

解釋 : 返回文件系統 一個塊的大小單位byte

方法 : getFreeBlocks ()

返回 : int

解釋 : 返回文件系統上剩餘的所有塊 包括預留的一般程序無法訪問的

方法 : restat (String path)

返回 : void

解釋 : 執行一個由該對象所引用的文件系統雷斯塔特.(Google翻譯)


想計算SDCard大小和使用情況時, 只需要得到SD卡總共擁有的Block數或是剩餘沒用的Block數,再乘以每個Block的大小就是相應的容量大小了單位byte.(見代碼)

  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 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章