首页 > 代码库 > Android开发学习---如何写数据到外部存储设备(sd卡),Environment.getExternalStorageDirectory,怎么获取sd卡的大小?

Android开发学习---如何写数据到外部存储设备(sd卡),Environment.getExternalStorageDirectory,怎么获取sd卡的大小?

本文主要介绍如何写数据到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();
        }

    }

}
View Code

 如何获取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).

获取其块大小,块数量.