安卓的所有应用都有一个放置在沙盒(sandbox)中的文件目录。文件保存在沙盒中可以阻止别的应用的访问,甚至是其他用户的窥探。
但是有时候我们也需要共享一些文件(比如图片,音频,视频等),这时候我们可以将这些数据文件存储在外部存储介质上,比如SD卡。
应用目录中存取数据
文件的存取不外乎就是通过文件路径,建立一个输入输出流,然后进行数据的读写。
如果我们需要直接在应用的沙盒目录中存取数据,我们可以直接调用系统自带的函数:Context.openFileOutput(…)和Context.openFileInput(…)。该方法接受文件名以及文件操作模式参数,会自动将传入的文件名附加到应用沙盒文件目录路径之后,形成一个新路径,然后在新路径下创建并打开文件,等待数据写入。如选择手动获取私有文件目录并在其下创建和打开文件,记得总是使用Context.getFilesDir()替代方法。
而且在具体的实现中,我们一般会把数据格式化成适当的形式,然后再转换成字符或字节数据存取到文件中。通常我们常用的两种存取数据的格式就是JSON和XML。安卓的开发包中都已经集成了这两种数据格式的相关包。
写出数据的简单操作:
Writer writer = null;
writer = new OutputStreamWriter(myAppContext.openFileOutput(mFileName, Context.MODE_PRIVATE));
writer.write(json.toString());
读入数据的简单操作:
StringBuilder sb = new StringBuilder();
BufferedReader reader = null;
String line;
reader = new BufferedReader(
new InputStreamReader(myAppContext.openFileInput(mFileName)));
while((line = reader.readLine()) != null)
sb.append(line);
外部设备存储
外部设备的存储一般主要是SD的文件存储。
在SD卡的文件存储中,我们首先要做的就是要设置应用获取SD卡的相关操作权限。
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
然后就是结合JAVA的相关I/O操作,进行一些常规的文件操作。对此简单封装了一个类实现在SD卡中进行文件读取操作。(暂时没支持SD卡中创建目录文件)
import android.os.Environment;
import android.util.Log;
import java.io.*;
/**
* Created by Bingo on 2015/9/16.
*/
public class SDCardHelper {
private static String TAG_SDCARD = "SDCardHelper";
/**
* 当存在SD卡,且SD卡挂载成功,可以读写时返回true,否则返回false
*/
public static boolean hasSDCard(){
if (android.os.Environment.getExternalStorageState()
.equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
/**
* 写出数据到文件
* @param fileName 文件名格式如下:“xxx.xxx”
* @param data
* @throws IOException
*/
public static void write2SDCard(String fileName, String data) throws IOException {
if(!hasSDCard()) {
Log.d(TAG_SDCARD, "sdcard doesn't exist.");
return;
}
File filePath = createSDFile(fileName);
filePath.setWritable(true);
Writer writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(filePath));
writer.write(data);
} catch (FileNotFoundException e) {
Log.d(TAG_SDCARD, "write2SDCard() file doesn't exist.");
e.printStackTrace();
}
finally {
if(writer != null)
writer.close();
}
}
public static String readFromSDCard(String fileName) throws IOException {
if(!hasSDCard()) {
Log.d(TAG_SDCARD, "sdcard doesn't exist.");
return null;
}
File filePath = createSDFile(fileName);
BufferedReader reader = null;
StringBuilder sb = new StringBuilder();
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath)));
String line;
while((line = reader.readLine()) != null)
sb.append(line);
} catch (FileNotFoundException e) {
Log.d(TAG_SDCARD, "readFromSDCard() file doesn't exist.");
e.printStackTrace();
}
finally {
if(reader != null)
reader.close();
}
return sb.toString();
}
public static File SDCardRoot(){
return android.os.Environment.getExternalStorageDirectory();
}
private static File createSDFile(String fileName) throws IOException {
File file = new File(SDCardHelper.SDCardRoot(), fileName);
if(!file.exists())
file.createNewFile();
return file;
}
}
其中有些函数是很关键的用来检测SD是否存在。
比如说android.os.Environment.getExternalStorageState()方法,将会返回当前外部存储的状态,具体状态值有哪些可以参考详细文档内容。
android.os.Environment.getExternalStorageState()则会返回SD卡的根目录。