想把文件保存到SD卡中,一定要知道SD卡的路径,有人说可以用File explore来查看,这种方法不太好,因为随着android版本的升级,SD卡的路径可能会发生改变。在1.6的时候SD的路径是/sdCard。后续版本都改成了mnt/sdCard。所有还是使用API来获取:

Environment.getExternalStorageDirectory()

另外,在保存之前要判断SD卡是否已经安装好,并且可读写:

//判断SDcard是否存在并且可读写
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
service.saveToSDCard(filename,filecontent);
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
}else{
Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show();
}

查看完整代码:

	/**
* 保存到SD卡
* @param filename
* @param filecontent
* @throws Exception
*/
public void saveToSDCard(String filename, String filecontent)throws Exception{
File file = new File(Environment.getExternalStorageDirectory(),filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(filecontent.getBytes());
outStream.close();
}
	@Override
public void onClick(View v) {
EditText filenameText = (EditText)findViewById(R.id.filename);
EditText filecontentText = (EditText)findViewById(R.id.filecontent);
String filename = filenameText.getText().toString();
String filecontent = filecontentText.getText().toString();
FileService service = new FileService(getApplicationContext());
try {
//判断SDcard是否存在并且可读写
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
service.saveToSDCard(filename,filecontent);
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
}else{
Toast.makeText(getApplicationContext(), R.string.sdcarderror, 1).show();
} } catch (Exception e) {
Toast.makeText(getApplicationContext(), R.string.fail, 1).show();
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), R.string.success, 1).show();
}

android保存文件到SD卡中的更多相关文章

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

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

  2. 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)

    1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...

  3. android 写文件到sd卡问题小记

    android 写文件到sd卡问题小记 事情是这样子的.... 这天我开始编写项目调试工具,高大上不?-----其实就是记录实时网络请求和崩溃日志相关等的小工具(此处一个会心的微笑). 然后我是这样写 ...

  4. android中拷贝assets下的资源文件到SD卡中(可以超过1M)

    很多手机游戏,在安装APK之后都得需要下载相应的资源包,然后才能进入游戏. 有这样一个需求:就是游戏中需要的资源包打在APK内,随apk一起进行安装到手机中. 这样就不需要,在安装APK之后,去下载资 ...

  5. Android -- 拷贝assets下的资源文件到SD卡中(可以超过1M)

    很多手机游戏,在安装APK之后都得需要下载相应的资源包,然后才能进入游戏. 有这样一个需求:就是游戏中需要的资源包打在APK内,随apk一起进行安装到手机中. 这样就不需要,在安装APK之后,去下载资 ...

  6. Android NDK加载SD卡中的so

    最近公司框架刚移植完成,由于框架程序要调用子程序,每个子程序都是一个so文件,有好几百个,把所有的so和apk打包不现实,及时可以升级维护也很麻烦.所以需要放SD卡中.考虑两种方式 1 放到设备中的 ...

  7. cygwin下烧写文件到sd卡中

    在cygwin下将firmware_sdcard.bin写入到sd卡中(cygwin需要以管理员身份启动) 1查看sd分区情况 cat /proc/partitions  (为了找到sd卡的标记) 2 ...

  8. Android下载文件到SD卡

    HttpURLConnection 上传方式: 尝试理解这两种流的区别: InputStreamReader 的读取方式: //创建一个URL对象 URL url = new URL(urlStrin ...

  9. Android -- 写xml到SD卡中

    信息类                                                                                          private ...

随机推荐

  1. (Problem 22)Names scores

    Using names.txt (right click and 'Save Link/Target As...'), a 46K text file containing over five-tho ...

  2. TCP/IP学习笔记

    1. 2. >>>   int socket(int af, int type,int protocol);//创建套接字,返回从文件描述表中取出新的索引号(int);AF_INET ...

  3. QR Code 码

    一.QR Code码 由日本Denso公司于1994年9月研制的一种矩阵二维码符号,它除具有一维条码及其它二维条码所有的信息容量大.可靠性高.可表示汉字及图象多种文字信息.保密防伪性强等优点外,还具有 ...

  4. wiki oi 3115高精度练习之减法

    题目描述 Description 给出两个正整数A和B,计算A-B的值.保证A和B的位数不超过500位. 输入描述 Input Description 读入两个用空格隔开的正整数 输出描述 Outpu ...

  5. STL vector使用方法介绍

    介绍 这篇文章的目的是为了介绍std::vector,怎样恰当地使用它们的成员函数等操作.本文中还讨论了条件函数和函数指针在迭代算法中使用,如在remove_if()和for_each()中的使用.通 ...

  6. 移动端rem,scale动态设置

    pt:物理像素(电容屏上像素块个数) px:逻辑像素.设备独立像素 高清屏:1px = 4pt 普通屏:1px = 1pt dpr:设备像素比:(某一方向上)物理像素/逻辑像素 通常设置1rem=屏幕 ...

  7. linux内核代码container_of

    它的作用显而易见,那就是根据一个结构体变量中的一个域成员变量的指针来获取指向整个结构体变量的指针. typedef unsigned int __kernel_size_t; typedef __ke ...

  8. <%@ include file=” ”%> ——最简洁易懂的解释

    <%@ include file=” ”%> 假如 在B.jsp 中,使用 <%@ include file=”A.jsp”%>  ,那么就是把 A.jsp 的内容 原封不动 ...

  9. iOS7,8 presentViewController 执行慢

    解决办法: 1, 使用GCD用主线程跳转 dispatch_async(dispatch_get_main_queue(), ^{ //跳转代码 ... }); 2, 召唤主线程, 使用perform ...

  10. c语言中malloc realloc 和calloc的联系与区别

    (1)C语言跟内存分配方式 <1>从静态存储区域分配.       内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量.static变量.<2> ...