1.文件写入SD卡
1.1 获取SD卡路径要使用
Environment.getExternalStorageDirectory()
1.2 判断SD卡状态
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
1.3 写入SD卡所需权限
android.permission.WRITE_EXTERNAL_STORAGE
1.4 dome
1.onClick事件
Log.i(TAG, "写入SD");
try {
service.writeFileToSDCard(name,context);
Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
2.fileService的writeFileToSDCard方法
public void writeFileToSDCard(String name, String context) throws IOException {
File file = new File(Environment.getExternalStorageDirectory()+name);
FileOutputStream fos = new FileOutputStream(file);
fos.write(context.getBytes());
fos.close();
}