Android入门开发之SD卡读写操作(转)
SD卡的读写是我们在开发android 应用程序过程中最常见的操作。下面介绍SD卡的读写操作方式:
1. 获取SD卡的根目录
- String sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
2. 在SD卡上创建文件夹目录
- /**
- * 在SD卡上创建目录
- */
- public File createDirOnSDCard(String dir)
- {
- File dirFile = new File(sdCardRoot + File.separator + dir +File.separator);
- Log.v("createDirOnSDCard", sdCardRoot + File.separator + dir +File.separator);
- dirFile.mkdirs();
- return dirFile;
- }
3. 在SD卡上创建文件
- /**
- * 在SD卡上创建文件
- */
- public File createFileOnSDCard(String fileName, String dir) throws IOException
- {
- File file = new File(sdCardRoot + File.separator + dir + File.separator + fileName);
- Log.v("createFileOnSDCard", sdCardRoot + File.separator + dir + File.separator + fileName);
- file.createNewFile();
- return file;
- }
4.判断文件是否存在于SD卡的某个目录
- /**
- * 判断SD卡上文件是否存在
- */
- public boolean isFileExist(String fileName, String path)
- {
- File file = new File(sdCardRoot + path + File.separator + fileName);
- return file.exists();
- }
5.将数据写入到SD卡指定目录文件
- <span style="white-space:pre"> </span>/**
- * 写入数据到SD卡中
- */
- public File writeData2SDCard(String path, String fileName, InputStream data)
- {
- File file = null;
- OutputStream output = null;
- try {
- createDirOnSDCard(path); //创建目录
- file = createFileOnSDCard(fileName, path); //创建文件
- output = new FileOutputStream(file);
- byte buffer[] = new byte[2*1024]; //每次写2K数据
- int temp;
- while((temp = data.read(buffer)) != -1 )
- {
- output.write(buffer,0,temp);
- }
- output.flush();
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally{
- try {
- output.close(); //关闭数据流操作
- } catch (Exception e2) {
- e2.printStackTrace();
- }
- }
- return file;
- }
one more important thing:
对SD卡的操作,必须要申请权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
转自:http://blog.csdn.net/newjerryj/article/details/8829179
Android入门开发之SD卡读写操作(转)的更多相关文章
- Android开发之SD卡上文件操作
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- android 64 sd卡读写的操作
package com.itheima.writesd; import java.io.File; import java.io.FileNotFoundException; import java. ...
- Android安全开发之ZIP文件目录遍历
1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...
- Android 数据库SQLite 写入SD卡
如果手机没有root,数据库文件是无法查看到的,不方便调试. 最好的办法是把数据库写进SD卡. 修改的地方有两处: 1.在你的helper类中把数据库文件名称 DATABASE_NAME 由原来的一个 ...
- Android 安全开发之 ZIP 文件目录遍历
1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在"../"的字符串,攻击者可以利用多个"../"在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原 ...
- Android安全开发之WebView中的地雷
Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...
- 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(七)-准备移植FatFs
[STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 [STM3 ...
- [android] 保存文件到SD卡
/****************2016年5月4日 更新*****************************/ 知乎:为什么很多Android应用要把文件写到/sdcard目录下而不是写到/d ...
- 第36章 SDIO—SD卡读写测试
第36章 SDIO—SD卡读写测试 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/f ...
随机推荐
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- C#之使用NotifyIcon实现任务栏托盘菜单,图标闪烁效果及气泡提示
很多程序是只需要后台运行的,甚至不需要自己的应用界面.NotifyIcon提供了程序在任务栏的显示功能 程序下载链接如下: http://download.csdn.net/detail/u01031 ...
- POJ 1661
http://poj.org/problem?id=1661 这是一道DP的题目,求最优解 上面的这一个题是对于那个重左边开始上的函数的解释 题目要求的是从最高掉下来的小时间,那么我们就可以求从最低处 ...
- ssh免密码登陆设置
服务器端 CentOS 6.5下编辑/etc/ssh/sshd_config MacOSx下编辑/etc/sshd_config #开启公钥验证 RSAAuthentication yes Pubke ...
- ios awakeFromNib 和 initWithCoder:
During the instantiation process, each object in the archive is unarchived and then initialized with ...
- python2.79安装
从官网下载最新的安装程序,基于windows的,也可以直接百度下载 点击安装,如果其他用户不需要python的话,可以使用第二个,不过我们一般都是单用户,所以没差 选择安装路径,可按默认路径安装,也可 ...
- Effective C++ -----条款28:避免返回handles指向对象内部成分
避免返回handles(包括reference.指针.迭代器)指向对象内部.遵守这个条款可增加封装性,帮助const成员函数的行为像个const,并将发生“虚吊号码牌”(dangling handle ...
- c#操作时间
本年还剩下多少天 private string GetEndTime() { DateTime dt = DateTime.Now; DateTime startYear = DateTime.Now ...
- EF的各种删除方法
//2.1检查 id 是否存在 //2.2执行删除 Models.Student stu = new Models.Student() { Id = id }; //db.Students.Attac ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...