1.说明:
>应用运行用到的数据文件(如图片)可以保存到sd卡中
>文件类型: 任意
>数据保存的路径:
--- 路径1:/storage/sdcard/Android/data/packageName/files/
--- 路径2: /storage/sdcard/xxx/
>路径1:其它应用可以访问,应用卸载时删除
>路径2: 其它应用可以访问,应用卸载时不会删除
>必须保证sd卡挂载在手机上才能读写,否则不能操作
2.相关API
1). Environment : 操作SD卡的工具类
> 得到SD卡的状态 : Environment.getExternalStorageState()
> 得到SD卡的路径 :Environment.getExternalStorageDirectory()
> SD卡可读写的挂载状态值 :Environment.MEDIA_MOUNTED
2). context.getExtermalFilesDir():
> 得到 /mnt/sdcard/Android/data/packageName/files/xxx.txt
3). android.permission.WRITE_EXTERNAL_STORAGE
3.相关代码
(代码中为方便用throw,应该用try)
public class OfActivity extends Activity {
private EditText et_of_name;
private EditText et_of_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_of);
et_of_name = (EditText) findViewById(R.id.et_of_name);
et_of_content = (EditText) findViewById(R.id.et_of_content);
}
/*
* 路径一保存
*/
public void save(View v) throws IOException {
// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 2. 读取输入的文件名/内容
String fileName = et_of_name.getText().toString();
String content = et_of_content.getText().toString();
// 3. 得到指定文件的OutputStream
// 1).得到sd卡下的files路径
File filesPath = getExternalFilesDir(null);
// 2).组成完整路径
String filePath = filesPath + "/" + fileName;
// 3).创建FileOutputStream
FileOutputStream fos = new FileOutputStream(filePath);
// 4.写数据
fos.write(content.getBytes("utf-8"));
fos.close();
// 5.提示
Toast.makeText(this, "保存完成", 0).show();
} else {
Toast.makeText(this, "sd卡没有挂载", 0).show();
}
}
/*
* 路径一读取
*/
public void read(View v) throws IOException {
// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 2. 读取输入的文件名/内容
String fileName = et_of_name.getText().toString();
// 3. 得到指定文件的InputStream
// 1).得到sd卡下的files路径
File filesPath = getExternalFilesDir(null);
// 2).组成完整路径
String filePath = filesPath + "/" + fileName;
// 3).创建FileInputStream
FileInputStream fis = new FileInputStream(filePath);
// 4.读取数据,成String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String content = baos.toString();
baos.close();
fis.close();
// 5.显示
et_of_content.setText(content);
} else {
Toast.makeText(this, "sd卡没有挂载", 0).show();
}
}
/*
* 路径二保存
*/
public void save2(View v) throws IOException {
// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 2. 读取输入的文件名/内容
String fileName = et_of_name.getText().toString();
String content = et_of_content.getText().toString();
// 3. 得到指定文件的OutputStream
// 1). /storage/sdcard/
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
// 2). /storage/sdcard/atguigu/ (创建文件夹)
File file = new File(sdPath + "/atguigu");
if (!file.exists()) {
file.mkdirs();
}
// 3). /storage/sdcard/atguigu/xxx.txt
String filePath = sdPath + "/atguigu" + fileName;
// 4).创建输出流
FileOutputStream fos = new FileOutputStream(filePath);
// 4.写数据
fos.write(content.getBytes("utf-8"));
fos.close();
// 5.提示
Toast.makeText(this, "保存完成", 0).show();
} else {
Toast.makeText(this, "sd卡没有挂载", 0).show();
}
}
/*
* 路径二读取
*/
public void read2(View v) throws IOException {
// 1.判断sd卡状态,如果是挂载的状态才继续,否则提示
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 2. 读取输入的文件名/内容
String fileName = et_of_name.getText().toString();
// 3. 得到指定文件的InputStream
String sdPath = Environment.getExternalStorageDirectory()
.getAbsolutePath();
String filePath = sdPath + "/atguigu" + fileName;
FileInputStream fis = new FileInputStream(filePath);
// 4.读取数据,成String
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
String content = baos.toString();
baos.close();
fis.close();
// 5.显示
et_of_content.setText(content);
} else {
Toast.makeText(this, "sd卡没有挂载", 0).show();
}
}
}