Android開發學習---如何寫數據到外部存儲設備(sd卡),Environment.getExternalStorageDirectory,怎麼獲取sd卡的大小?

本文主要介紹如何寫數據到sd卡,這裏主要到的技術是Environment中的方法.java

1.android

 

 

2.實現代碼:緩存

 /datasave/src/com/amos/datasave/savePasswordService.java ide

    //寫數據到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類.this

android.os.Environmentspa

其主要方法有:code

  • 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();
        }

    }

}
View Code

 如何獲取sdcard的大小?blog

        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).rem

獲取其塊大小,塊數量.get

相關文章
相關標籤/搜索