Android SD卡操作

sdcard讀寫 默認android系統對每個app都開放讀寫功能。

默認路徑爲/mnt/sdcard/ 或者/sdcard/ 
寫外部存儲即sdcard的權限

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

打開關閉sdcard的權限

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


http://www.itivy.com/android/archive/2011/7/4/android-sd-card-operation-file-and-capacity-compute.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 操作    
  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 操作  
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的容量大小
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 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章