原文連接: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"
解釋 : 已經掛載並且擁有可讀可寫權限
值 : "mounted_ro"
解釋 : 已經掛載,但只擁有可讀權限
常量 : MEDIA_NOFS
值 : "nofs"
解釋 : 對象空白,或者文件系統不支持
常量 : MEDIA_REMOVED
值 : "removed"
解釋 : 已經移除擴展設備
常量 : MEDIA_SHARED
值 : "shared"
解釋 : 如果SDCard未掛載,並通過USB大容量存儲共享
常量 : MEDIA_UNMOUNTABLE
值 : "unmountable"
解釋 : 不可以掛載任何擴展設備
常量 : MEDIA_UNMOUNTED
值 : "unmounted"
解釋 : 已經卸載
使用時只需先判斷SDCard當前的狀態然後取得SdCard的目錄即可(見源代碼)
-
//SDcard 操作 //SDcard 操作
-
- ublic void SDCardTest() {
//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的容量大小
- 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的容量大小
android.os.StatFs
一個模擬linux的df命令的一個類,獲得SD卡和手機內存的使用情況
java.lang.Object |
|
|
|
android.os.StatFs |
構造方法:
公用方法:
方法 : getAvailableBlocks ()
返回 : int
解釋 :返回文件系統上剩下的可供程序使用的塊
方法 : getBlockCount ()
返回 : int
解釋 : 返回文件系統上總共的塊
方法 : getBlockSize ()
返回 : int
解釋 : 返回文件系統 一個塊的大小單位byte
方法 : getFreeBlocks ()
返回 : int
解釋 : 返回文件系統上剩餘的所有塊 包括預留的一般程序無法訪問的
返回 : void
解釋 : 執行一個由該對象所引用的文件系統雷斯塔特.(Google翻譯)
想計算SDCard大小和使用情況時, 只需要得到SD卡總共擁有的Block數或是剩餘沒用的Block數,再乘以每個Block的大小就是相應的容量大小了單位byte.(見代碼)
- public void SDCardSizeTest() {
- // 取得SDCard當前的狀態
- String sDcString = android.os.Environment.getExternalStorageState();
- if (sDcString.equals(android.os.Environment.MEDIA_MOUNTED)) {
- // 取得sdcard文件路徑
- File pathFile = android.os.Environment
- .getExternalStorageDirectory();
- android.os.StatFs statfs = new android.os.StatFs(pathFile.getPath());
- // 獲取SDCard上BLOCK總數
- long nTotalBlocks = statfs.getBlockCount();
- // 獲取SDCard上每個block的SIZE
- long nBlocSize = statfs.getBlockSize();
- // 獲取可供程序使用的Block的數量
- long nAvailaBlock = statfs.getAvailableBlocks();
- // 獲取剩下的所有Block的數量(包括預留的一般程序無法使用的塊)
- long nFreeBlock = statfs.getFreeBlocks();
- // 計算SDCard 總容量大小MB
- long nSDTotalSize = nTotalBlocks * nBlocSize / 1024 / 1024;
- // 計算 SDCard 剩餘大小MB
- long nSDFreeSize = nAvailaBlock * nBlocSize / 1024 / 1024;
- }// end of if
- // end of func