读写SD卡上的文件
来源:互联网 发布:海马玩mac版 编辑:程序博客网 时间:2024/05/29 03:19
public String getFileFromSdcard(String fileName) { // 缓存的流,和磁盘无关,不需要关闭 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { File file = new File(Environment.getExternalStorageDirectory(), fileName); FileInputStream inputStream = null; 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) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } return new String(outputStream.toByteArray());}/** * @param fileName 文件的名称 * @param content 文件的内容 * @return */public boolean saveContentToSdcard(String fileName, String content) { boolean flag = false; // 判断是否有SD卡,且应用具有读写权限 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { // 获得sdcard卡所在的路径 File sdCardDir = Environment.getExternalStorageDirectory(); File file = new File(sdCardDir, fileName); FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); flag = true; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (fileOutputStream != null) { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } return flag;}
<!-- 添加读写sdcard卡的授权 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
http://byandby.iteye.com/blog/835671
gg
- 读写SD卡上的文件
- 读写SD卡上的文件
- 读写SD卡上的文件
- 读写SD卡上的文件
- android读写SD卡上的文件
- 读写SD卡上的文件
- 读写SD卡上的文件
- SD卡上读写文件
- 读写sd卡的文件
- Android读写SD卡上的数据
- SD卡读写文件
- 读写SD卡文件
- sd卡读写文件
- SD卡文件读写
- Android的SD卡文件读写
- Android的SD卡文件读写
- Android 读写SD卡的文件
- Android的SD卡文件读写操作
- linux 下开放指定端口
- 代码走查规范表
- STL之缓冲区迭代器
- Matlab如何找出两个矩阵中相同的元素
- 调整viewState的位置,有助于SEO
- 读写SD卡上的文件
- k-means
- 聚集索引和非聚集索引
- 图片转换成Base64编码集成到html文件
- UIView上添加了一个按钮和一个单击手势的事件相应,互相不影响的处理方法。。
- Source Insight:使用Source Insight查看C/C++源代码
- 用Eclipse做Android开发时出现java.lang.NoClassDefFoundError问题
- 拷贝构造函数
- opencv 2.1 与opencv 2.4.4静态编译(主要是opencv 2.1)