android sd 卡操作

来源:互联网 发布:免费的电视直播软件 编辑:程序博客网 时间:2024/04/30 00:38
//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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    
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