android写入文件

来源:互联网 发布:论文数据来源怎么标注 编辑:程序博客网 时间:2024/05/22 14:42

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();}

2.文件写入ROM

3.SharedPreferences

0 0
原创粉丝点击