当前位置: 代码迷 >> Android >> 小弟我的android 第9天 - 文件存储
  详细解决方案

小弟我的android 第9天 - 文件存储

热度:111   发布时间:2016-04-28 07:17:55.0
我的android 第9天 - 文件存储

数据存储与访问

? 很多时候我们的软件需要对处理后的数据进行存储或再次访问。Android为数据存储提供了多种方式,分别有如下几种:

??? 文件(txt\xml)

??? SharedPreferences(参数)

??? SQLite数据库

??? 内容提供者(Content provider

??? 网络

介绍:文件存储是Android开发中常用的操作,本节主要针对文件在手机内存卡以及SDCard的存储实现

案例:在Activity界面中实现对文件的存储与读取

一、场景分析

?

?控件:2EditText2Button
?场景:当点击保存,把用户写入的内容存入到指定的文件名中(内存与sdcard

?????? 当点击读取,从内存或者sdcard中读取指定的文件到文本框中

?

?

? 二?实现

/**

? ?* 保存文件到手机自带存储空间

? ?* @paramfileName文件名

? ?* @paramfileBody文件内容

? ?*/

? public void save(String fileName, String fileBody) throws Exception{

? // 文件名不能包含路径 , 文件保存在 /data/data/包名/files

? FileOutputStreamfos = context.openFileOutput(fileName, Context.MODE_PRIVATE);

? fos.write(fileBody.getBytes());

? fos.close();

? }

?

? /**

? ?* 读取文件内容

? ?* @paramfileName文件名

? ?* @return 文件内容

? ?*/

? public String read(String fileName) throws Exception {

? // 会读取 /data/data/包名/files文件夹下的文件

? FileInputStreamfis = context.openFileInput(fileName);

?

? ByteArrayOutputStreambaos = new ByteArrayOutputStream();

?

? byte[] buffer = new byte[1024];

? intlen = -1;

? while ( (len = fis.read(buffer)) != -1) {

? baos.write(buffer, 0, len);

? }

? fis.close();

? baos.close();

?

? return new String(baos.toByteArray());

? }

?

? /**

? ?* 保存文件到SDCard

? ?*/

? public void saveToSDCard(String fileName, String fileBody) throws Exception {

? if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

? // 得到SDCard根路径

? File rootDir = Environment.getExternalStorageDirectory();

? File file = new File(rootDir, fileName);

? FileOutputStreamfos = new FileOutputStream(file);

? fos.write(fileBody.getBytes());

? fos.close();

? } else {

? throw new RuntimeException("SDCard无法正常使用");

? }

?

? }

1.手机自带存储空间

1> 存储

?* context.openFileOutput(String name, int mode)

?? * name : 文件名(不能包含路径)

?? * mode : 文件操作模式

???? - Context.MODE_PRIVATE

??????? * 只能被本应用读写

? * 新写入的内容会覆盖原文件内容

???? - Context.MODE_APPEND

??????? * 只能被本应用读写

? * 新写入的内容会追加到原文件内容后面

???? - Context.MODE_READABLE

??????? * 允许其他应用读取,不可以写入

? * 新写入的内容会覆盖原文件内容

???? - Context.MODE_WRITEABLE

??????? * 允许其他应用写入,不可以读取

? * 新写入的内容会覆盖原文件内容

??? (如果要求其他应用可以对文件进行读和写,那就Context.MODE_READABLE + Context.MODE_WRITEABLE)

?

?* 存储到/data/data/包名/files/文件名

?

2> 访问

?* context.openFileInput(name)

?* 默认访问/data/data/包名/files/文件名

?* 通过context.getFilesDir()可以获取/data/data/包名/files这个路径

?* 通过context.getCacheDir()可以获取/data/data/包名/cache这个路径

?

3> 特点:

? * 存储的文件不能太大

? * 不依赖于SDCard

? * 当应用被卸载了,/data/data/包名? 这个目录也会被删除

?

2.SDCard

1> 检测SDCard状态

说明SDCard是可用的:Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

?

2> SDCard写入内容

?* 先添加权限:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

?* 得到SDCard根路径: File rootDir = Environment.getExternalStorageDirectory();

?* 生成输出流:

?File file = new File(rootDir, fileName);

?

?FileOutputStreamfos = new FileOutputStream(file);

?

下载视频代码

?

?

??

  相关解决方案