SD卡的读写是我们在开发android 应用程序过程中最常见的操作。下面介绍SD卡的读写操作方式:

1. 获取SD卡的根目录

  1. String  sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();

2. 在SD卡上创建文件夹目录

  1. /**
  2. * 在SD卡上创建目录
  3. */
  4. public File createDirOnSDCard(String dir)
  5. {
  6. File dirFile = new File(sdCardRoot + File.separator + dir +File.separator);
  7. Log.v("createDirOnSDCard", sdCardRoot + File.separator + dir +File.separator);
  8. dirFile.mkdirs();
  9. return dirFile;
  10. }

3. 在SD卡上创建文件

  1. /**
  2. * 在SD卡上创建文件
  3. */
  4. public File createFileOnSDCard(String fileName, String dir) throws IOException
  5. {
  6. File file = new File(sdCardRoot + File.separator + dir + File.separator + fileName);
  7. Log.v("createFileOnSDCard", sdCardRoot + File.separator + dir + File.separator + fileName);
  8. file.createNewFile();
  9. return file;
  10. }

4.判断文件是否存在于SD卡的某个目录

  1. /**
  2. * 判断SD卡上文件是否存在
  3. */
  4. public boolean isFileExist(String fileName, String path)
  5. {
  6. File file = new File(sdCardRoot + path + File.separator + fileName);
  7. return file.exists();
  8. }

5.将数据写入到SD卡指定目录文件

  1. <span style="white-space:pre">  </span>/**
  2. * 写入数据到SD卡中
  3. */
  4. public File writeData2SDCard(String path, String fileName, InputStream data)
  5. {
  6. File file = null;
  7. OutputStream output = null;
  8. try {
  9. createDirOnSDCard(path);  //创建目录
  10. file = createFileOnSDCard(fileName, path);  //创建文件
  11. output = new FileOutputStream(file);
  12. byte buffer[] = new byte[2*1024];          //每次写2K数据
  13. int temp;
  14. while((temp = data.read(buffer)) != -1 )
  15. {
  16. output.write(buffer,0,temp);
  17. }
  18. output.flush();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. finally{
  23. try {
  24. output.close();    //关闭数据流操作
  25. } catch (Exception e2) {
  26. e2.printStackTrace();
  27. }
  28. }
  29. return file;
  30. }

 one more important thing:

      对SD卡的操作,必须要申请权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

转自:http://blog.csdn.net/newjerryj/article/details/8829179

Android入门开发之SD卡读写操作(转)的更多相关文章

  1. Android开发之SD卡上文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  2. android 64 sd卡读写的操作

    package com.itheima.writesd; import java.io.File; import java.io.FileNotFoundException; import java. ...

  3. Android安全开发之ZIP文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...

  4. Android 数据库SQLite 写入SD卡

    如果手机没有root,数据库文件是无法查看到的,不方便调试. 最好的办法是把数据库写进SD卡. 修改的地方有两处: 1.在你的helper类中把数据库文件名称 DATABASE_NAME 由原来的一个 ...

  5. Android 安全开发之 ZIP 文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在"../"的字符串,攻击者可以利用多个"../"在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原 ...

  6. Android安全开发之WebView中的地雷

    Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...

  7. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs

    [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...

  8. [android] 保存文件到SD卡

    /****************2016年5月4日 更新*****************************/ 知乎:为什么很多Android应用要把文件写到/sdcard目录下而不是写到/d ...

  9. 第36章 SDIO—SD卡读写测试

    第36章     SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...

随机推荐

  1. Griddle, griddle-react 一个REACT 表格组件

    Griddle, griddle-react 一个REACT 表格组件: http://griddlegriddle.github.io/Griddle/index.html

  2. Js注释

    注释 介绍 作用 合作分享:方便他人阅读,便于分享 沉淀总结:容易忘记代码,自己总结沉淀 形式 1.// 双斜杠 2./**/斜杠星号 常用标签 标签 描述 @module 标明当前文件模块,在这个文 ...

  3. MySQL thread pool【转】

    本文来自:http://blog.chinaunix.net/uid-26896862-id-3993773.html 刚刚经历了淘宝的双11,真实感受到了紧张的氛围.尽管DB淡定的度过,但是历程中的 ...

  4. 3.SpringMVC修改配置文件路径和给界面传递数据

    1.修改配置文件路径  达到  配置多文件的目的 web.xml文件中基础配置有springMVC配置的servlet路径 <servlet-name>SpringMVC</serv ...

  5. ABAP 数值类型转换

    CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'       EXPORTING         input  = wa_zz-werks       IMPOR ...

  6. [转]收集android上开源的酷炫的交互动画和视觉效果:Interactive-animation

    原文链接:http://www.open-open.com/lib/view/open1411443332703.html 描述:收集android上开源的酷炫的交互动画和视觉效果. 1.交互篇 2. ...

  7. ajax,下拉框级联

    js代码: $(document).ready(function() { $("#type1").change(function(){ var type1Code=$(" ...

  8. jquery $(document).ready() 与window.onload的异同

    Jquery中$(document).ready()的作用类似于传统JavaScript中的window.onload方法,不过与window.onload方法还是有区别的.   1.执行时间     ...

  9. 【2016-09-16】UbuntuServer14.04或更高版本安装问题记录

    出于项目需要,我们的Qt程序需要运行在 1. Windows/Linux-X86平台(CPU为常见的桌面级CPU如G3220.I3等): 2. Windows/Linux-X86低功耗平台(CPU为I ...

  10. 创建DLL、Lib以及使用DLL、Lib

    1.要在生成DLL文件的同时生成Lib文件,函数声明时前面要加__declspec(dllexport). 可在头文件中如下定义: #ifndef __MYDLL_H#define __MYDLL_H ...