SdCard操作

将数据存到内存中

1
】存储
FileOutputStream out=openFileOutput(“my.txt”, MODE_PRIVATE);
out.write(content.getBytes());
】获取
FileInputStram in = openFileInput(“my.txt”);
byte b[]=new byte[1024];
int len=0;
String result=”“;
while((len=in.read(b))!=-1){
result+=new String(b,0,len);
}
】sd卡的操作

package com.example.day14_sdcard01;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

// 判断SDCard是否挂载
public void isMounted(View v) {
    boolean mounted = SDCardUtils.isMounted();
    if (mounted) {
        Toast.makeText(this, "SDCard已挂载!", 1).show();
    } else {
        Toast.makeText(this, "SDCard未挂载!", 1).show();
    }
}

// 获得sdCard 的路径
public void getPath(View v) {
    String path = SDCardUtils.getSDCardPath();
    Toast.makeText(this, path, 1).show();
}

// 获得scCard的大小
public void getSize(View v) {
    long size = SDCardUtils.getSize();
    Toast.makeText(this, size + "", 1).show();
}

// 获得sdcard可用大小
public void getAvailableSize(View v) {
    long size = SDCardUtils.getAvailableSize();
    Toast.makeText(this, size + "", 1).show();
}

// 保存文件
public void save(View v) {
    String data = "面向大海,春暖花开!";
    boolean isSaved = SDCardUtils.saveDataIntoSDCard(data.getBytes(),
            "1522", "1522.txt");
    if (isSaved) {
        Toast.makeText(this, "保存成功", 1).show();
    } else {
        Toast.makeText(this, "保存失败", 1).show();
    }
}

// 读取数据
public void read(View v) {
    byte[] b = SDCardUtils.getDataFromSDCard("1522", "1522.txt");
    String s = new String(b);
    Toast.makeText(this, s, 1).show();
}

// 保存数据到公共路径下
public void savePublic(View v) {
    String data = "面向大海,春暖花开!";
    boolean isSaved = SDCardUtils.saveDataIntoSDCardPublic(data.getBytes(),
            Environment.DIRECTORY_MUSIC, "a.mp3");
    if (isSaved) {
        Toast.makeText(this, "保存成功", 1).show();
    } else {
        Toast.makeText(this, "保存失败", 1).show();
    }
}

// 保存数据到私有路径下
public void savePrivate(View v) {
    String data = "美女相片";
    boolean isSaved = SDCardUtils
            .saveDataIntoSDCardPrivate(data.getBytes(), this,
                    Environment.DIRECTORY_DCIM, "beauty.jpg");
    if (isSaved) {
        Toast.makeText(this, "保存成功", 1).show();
    } else {
        Toast.makeText(this, "保存失败", 1).show();
    }
}

}

package com.example.day14_sdcard01;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

public class SDCardUtils {
// 判断SDCard是否挂载,true,挂载,false,未挂载
// Environment.MEDIA_MOUNTED,表示sdCard已经挂载
// Environment.getExternalStorageState(),获得当前sdCard的挂载状态
public static boolean isMounted() {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true;
}
return false;
}

// 获得SDCard 的路径
public static String getSDCardPath() {
    if (isMounted()) {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }
    return null;
}

// 获得scCard的大小
public static long getSize() {
    if (isMounted()) {
        // StatFs,用来计算文件系统存储空间大小的工具类
        StatFs stat = new StatFs(getSDCardPath());

        // 获得块的数量
        // int count = stat.getBlockCount();
        long count = stat.getBlockCountLong();
        // 获得每块的大小
        // int size = stat.getBlockSize();
        long size = stat.getBlockSizeLong();

        return count * size / 1024 / 1024;
    }
    return 0;
}

// 获得sdcard可用大小
public static long getAvailableSize() {
    if (isMounted()) {
        StatFs stat = new StatFs(getSDCardPath());
        // 可用的块数
        long availCount = stat.getAvailableBlocksLong();
        // 块的大小
        long size = stat.getBlockSizeLong();
        return availCount * size / 1024 / 1024;
    }
    return 0;
}

// 将文件保存到sdCard
// data,保存的数据
// dir,保存的路径
// fileName,保存的文件名
public static boolean saveDataIntoSDCard(byte[] data, String dir,
        String fileName) {
    if (isMounted()) {
        // 先判断存储的路径dir是否存在(创建),如果不存在,先创建
        String path = getSDCardPath() + File.separator + dir;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        // 如果存在,进行读写操作
        file = new File(path + File.separator + fileName);
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(data);
            bos.flush();

            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return false;
}

// 读取sdcard的数据
public static byte[] getDataFromSDCard(String dir, String fileName) {
    if (isMounted()) {
        String path = getSDCardPath() + File.separator + dir
                + File.separator + fileName;
        File file = new File(path);
        if (file.exists()) {
            ByteArrayOutputStream baos = null;
            BufferedInputStream bis = null;

            try {
                baos = new ByteArrayOutputStream();
                bis = new BufferedInputStream(new FileInputStream(file));

                byte[] b = new byte[1024 * 8];
                int n = 0;
                while ((n = bis.read(b)) != -1) {
                    baos.write(b, 0, n);
                    baos.flush();
                }
                return baos.toByteArray();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return null;
}

// 根据具体的类型获得公共路径
public static String getPublicPath(String type) {
    if (isMounted()) {
        return Environment.getExternalStoragePublicDirectory(type)
                .getAbsolutePath();
    }
    return null;
}

// 保存公共路径下
public static boolean saveDataIntoSDCardPublic(byte[] data, String type,
        String fileName) {
    if (isMounted()) {
        File file = new File(getPublicPath(type));
        if (!file.exists()) {
            file.mkdirs();
        }
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(new File(
                    file, fileName)));
            bos.write(data, 0, data.length);
            bos.flush();

            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return false;
}

// 根据上下文,以及路径的类型,返回私有的路径名
public static String getPrivatePath(Context context, String type) {
    if (isMounted()) {
        return context.getExternalFilesDir(type).getAbsolutePath();
    }
    return null;
}

// 保存到私有路径下
public static boolean saveDataIntoSDCardPrivate(byte[] data,
        Context context, String type, String fileName) {
    if (isMounted()) {
        File file = new File(getPrivatePath(context, type));
        if (!file.exists()) {
            file.mkdirs();
        }

        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(new File(
                    file, fileName)));
            bos.write(data, 0, data.length);
            bos.flush();
            return true;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                if (bos != null)
                    bos.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }
    return false;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值