本文主要介紹如何寫數據到sd卡,這里主要到的技術是Environment中的方法.
1.
2.實現代碼:
/datasave/src/com/amos/datasave/savePasswordService.java
//寫數據到sdcard public void savePasswordToSDCard(String name, String password) { // android 2.1 /sdcard/xx.txt // android 2.2 /mnt/sdcard/xx.txt // self_define /excard/xx.txt // File externalStorageDirectory = Environment.getExternalStorageDirectory(); // String path = externalStorageDirectory.getPath(); // System.out.println("path:" + path); // 要存儲的內容 String content = name + ":" + password; Log.d(tag, "檢驗sdcard是否可用?"); //判斷sdcard是否存在? if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Log.d(tag, "sdcard不可用!"); Toast.makeText(context, "沒有找到SDCard!", Toast.LENGTH_LONG); return ; }; try { // File file = new File("/sdcard/qqpassword.txt"); // File file = new File(path + "/qqpassword2.txt"); File file = new File(Environment.getExternalStorageDirectory(), "/qqpassword2.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write(content.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
在android2.1及以前版本中,其sdcard目錄在根目錄,2.2,2.3以后其sdcard目錄就變成了/mnt/sdcard了,以及一些廠商自定義的android系統,可能也會把sdcard的名稱改的各不相同.這里如果還是用絕對路徑,那么程序的兼容性將會大大降低.這里主要用到了Enviroment類.
android.os.Environment
其主要方法有:
- getRootDirectory()---->/ 獲取根目錄
- getDataDirectory()---->/data 獲取data目錄
- getExternalStorageDirectory()--->/sdcard 獲取sd卡目錄
- getDownloadCacheDirectory()--->/cache 獲取下載文件的緩存目錄
- getExternalStorageState--->sdcard的狀態,removed,unmounted,checking,nofs,mounted,mounted_ro,shared,unmountable,bad_removal
完整的savePasswordService.java文件為:

package com.amos.datasave; import java.io.File; import java.io.FileOutputStream; import android.annotation.SuppressLint; import android.content.Context; import android.os.Environment; import android.util.Log; import android.widget.Toast; @SuppressLint("WorldWriteableFiles") public class savePasswordService { private Context context; private String tag = "savePasswordService"; public savePasswordService(Context context) { this.context = context; } public void savePasswordToFile(String name, String password) { // 這里設置文件的權限 String content = name + ":" + password; Log.d(tag, "設置文件的讀寫權限"); try { FileOutputStream fileOutput = context.openFileOutput("LoginTestConfig.txt", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE); fileOutput.write(content.getBytes()); fileOutput.flush(); fileOutput.close(); } catch (Exception e) { Log.d(tag, "設置文件的讀寫權限失敗!"); e.printStackTrace(); } } //寫數據到sdcard public void savePasswordToSDCard(String name, String password) { // android 2.1 /sdcard/xx.txt // android 2.2 /mnt/sdcard/xx.txt // self_define /excard/xx.txt // File externalStorageDirectory = Environment.getExternalStorageDirectory(); // String path = externalStorageDirectory.getPath(); // System.out.println("path:" + path); // 要存儲的內容 String content = name + ":" + password; Log.d(tag, "檢驗sdcard是否可用?"); //判斷sdcard是否存在? if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ Log.d(tag, "sdcard不可用!"); Toast.makeText(context, "沒有找到SDCard!", Toast.LENGTH_LONG); return ; }; try { // File file = new File("/sdcard/qqpassword.txt"); // File file = new File(path + "/qqpassword2.txt"); File file = new File(Environment.getExternalStorageDirectory(), "/qqpassword2.txt"); FileOutputStream fos = new FileOutputStream(file); fos.write(content.getBytes()); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } } }
如何獲取sdcard的大小?
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath()); long blockSize = statFs.getBlockSize(); long blockCount = statFs.getBlockCount(); long sdCardSize = blockSize*blockCount; Log.d(tag,""+sdCardSize );
這里使用的是Environment中的方法獲取到sdcard的路徑,然后將其路徑通過StatFs類,該類主要獲取指定文件路徑下的文件信息(filesystem info).
獲取其塊大小,塊數量.