一、基础概要
1、说明:
1>应用程序运行用到的数据文件可以保存到sd卡中
2>文件类型:任意
3>数据保存路径:
路径1:/storage/sdcard/Android/data/packageName/files 其它应用可以访问,应用卸载时删除
路径2:/storage/sdcard/xxx/ (表示自己创建的文件--xxx) 其它应用可以访问,应用卸载时不会被删除
2、相关API
Environment 操作sd卡工具类:
---得到sd卡状态:Environment.getExternalStorageState()
sd卡可读写的挂载状态值:Environment.MEDIA_MOUNTED
---得到sd卡路径:Environment.getExternalStorageDirectory()
context.getExternalFilesDir():
---得到 /mnt/sdcard/Android/data/package_name/files/xxx.txt
操作sd卡的权限:
---android.permission.WRITE_EXTERNAL_STORAGE 属于写的权限,但加上后也可读。
二、开发步骤(路径1)
1、写数据
1>判断sd卡状态,如果是挂载的状态继续
2>获取输入文件名/内容
3>得到指定文件的OutputStream:
.得到sd卡下的files路径
.组成完整路径
.创建FileOutputStream
4>写数据
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String fileName="xrk";
String fileContent="我不是向日葵";
String filesPath=getExternalFilesDir(null).getAbsolutePath();
String filePath=filesPath+"/"+fileName;
FileOutputStream fos=new FileOutputStream(filePath);
fos.write(fileContent.getBytes("utf-8"));
fos.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "保存是啊比", Toast.LENGTH_SHORT).show();
}
2、读数据:
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String fileName="xrk";
String filesPath=getExternalFilesDir(null).getAbsolutePath();
System.out.println("iueiudshcs"+filesPath);
String filePath=filesPath+"/"+fileName;
FileInputStream fis=new FileInputStream(filePath);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=fis.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
String content=baos.toString();
Toast.makeText(MainActivity.this, "读取成功"+content, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "读取失败", Toast.LENGTH_SHORT).show();
}
三、开发步骤(路径2)
1、写数据
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String fileName="谢荣康.txt";
String fileContent="我不是向日葵";
//得到指定文件的输出流
String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath();
File file=new File(sdPath+"/atguigu");
if (!file.exists()) {
file.mkdirs();//创建文件夹
}
String filePath=sdPath+"/atguigu/"+fileName;
String filesPath=getExternalFilesDir(null).getAbsolutePath();
System.out.println("iueiudshcs"+filesPath);
FileOutputStream fos=new FileOutputStream(filePath);
fos.write(fileContent.getBytes("utf-8"));
fos.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "保存是啊比", Toast.LENGTH_SHORT).show();
}
2、读数据:
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String fileName="谢荣康.txt";
String sdPath=Environment.getExternalStorageDirectory().getAbsolutePath();
String filePath=sdPath+"/atguigu/"+fileName;
FileInputStream fis=new FileInputStream(filePath);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=fis.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
String content=baos.toString();
fis.close();
Toast.makeText(MainActivity.this, "读取成功"+content, Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "读取失败", Toast.LENGTH_SHORT).show();
}