Android中SdCard的数据读写

发布时间:2020-07-13 07:13:30 作者:just2012xia
来源:网络 阅读:289
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.R.integer;
import android.content.Context;
import android.os.Environment;
public class FileService {
    private Context context;
    public FileService(Context context) {
        this.context = context;
    }
    public FileService() {
    }
    public String getFileFromSdcard(String fileName) {
        FileInputStream inputStream = null;
        // 缓存的流,和磁盘无关,不需要关闭
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        File file = new File(Environment.getExternalStorageDirectory(),
                fileName);
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            try {
                inputStream = new FileInputStream(file);
                int len = 0;
                byte[] data = new byte[1024];
                while ((len = inputStream.read(data)) != -1) {
                    outputStream.write(data, 0, len);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (inputStream != null) {
                    try {
                        inputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return new String(outputStream.toByteArray());
    }
    /**
     * @param fileName
     *            文件的名称
     * @param content
     *            文件的内容
     * @return
     */
    public boolean saveContentToSdcard(String fileName, String content) {
        boolean flag = false;
        FileOutputStream fileOutputStream = null;
        // 获得sdcard卡所在的路径
        File file = new File(Environment.getExternalStorageDirectory(),
                fileName);
        // 判断sdcard卡是否可用
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            try {
                fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(content.getBytes());
                flag = true;
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (fileOutputStream != null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return flag;
    }
}


Manifest文件

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


推荐阅读:
  1. Android 5.0+删除Sdcard文件
  2. android侦听USB或者SDCard热插拔事件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sdcard roi car

上一篇:Spring入门导读——IoC和AOP

下一篇:js实现的展开与收起1

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》