上次写了Android有关文件存取的常用操作。

在Android上开发时还会遇到将文件在SDCard上的存取操作。

下面是将数据写入到SDCard中的操作:

1 public void saveToSDCard(String filename, String content)throws Exception {2         File file = new File(Environment.getExternalStorageDirectory(), filename);3         FileOutputStream outStream = new FileOutputStream(file);4         outStream.write(content.getBytes());5         outStream.close();6     }

 1 try { 2                //判断SDCard是否存在,并且可以读写 3                 if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ 4                     saveToSDCard(filename, content); 5                     Toast.makeText(getApplicationContext(), R.string.success, 1).show(); 6                 }else{ 7                     Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show(); 8                 }                 9             } catch (Exception e) {10                 Toast.makeText(getApplicationContext(), R.string.fail, 1).show();11                 e.printStackTrace();12             }

首先需要判断SDCard是否可写(考虑写保护)以及是否存在等情况,进行环境检测,然后再写入存储卡。

更多相关文章

  1. Android操作系统简介
  2. Android学习笔记(六)-文件操作与SDCard读写访问
  3. Android SQLite 数据库 增删改查操作
  4. Android Day03-SQLite数据库操作及ListView详解
  5. 【android 串口开发(二) 之 串口读写操作】
  6. Android中SQLite数据库操作(2)——SQLiteOpenHelper类
  7. Android studio cmd命令行操作数据库文件导出,导入

随机推荐

  1. 【移动开发】Android应用开发者应该知道
  2. android文档查询小工具
  3. Unity3d和Android的互相调用(二)
  4. Android(安卓)APK反编译查看源码及资源文
  5. BlockCanary分析android卡顿
  6. android下的内存泄漏
  7. android用户界面-事件处理
  8. mipmap和drawable的区别
  9. Android(安卓)NestedScrolling解决滑动冲
  10. 从0开始学Android之启动模式