1、main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <EditText
android:id="@+id/numET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入文件名" /> <EditText
android:id="@+id/contentET"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入文件内容"//提示文本
android:inputType="textMultiLine"
android:minLines="3" />//最小为3行,多了自动变大 <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button android:id="@+id/sdcBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到sd卡"
android:onClick="onClick"
/> <Button android:id="@+id/romBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存到手机"
android:onClick="onClick"
/>
</LinearLayout> </LinearLayout>

2、MainActivity

package com.njupt.file1;

import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { private EditText numET;
private EditText contentET; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); numET = (EditText) findViewById(R.id.numET);
contentET = (EditText) findViewById(R.id.contentET); } @Override
protected void onResume() {
super.onResume(); //Environment.getExternalStorageState().获取sd卡的状态,判断是安装,还是移除
findViewById(R.id.sdcBT).setEnabled(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED));
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} public void onClick(View v){
try{
String name = numET.getText().toString();
String content = contentET.getText().toString(); FileService service = new FileService(); switch(v.getId()){
case R.id.sdcBT: service.saveToSDCard(name,content);
break; case R.id.romBT: service.saveToRom(name,content);
break;
} Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getApplication(), "dacard出异常", Toast.LENGTH_SHORT).show();
}
} }

3、FileService

package com.njupt.file1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import android.os.Environment; public class FileService { public void saveToSDCard(String name, String content) { FileOutputStream fos = null; try{ //Environment.getExternalStorageDirectory()。获取sd卡的路径
File file = new File(Environment.getExternalStorageDirectory(),name);
fos = new FileOutputStream(file); fos.write(content.getBytes());
}catch(Exception e){ e.printStackTrace(); }finally{ try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} public void saveToRom(String name, String content) {
// TODO Auto-generated method stub } }

4、AndroidManifest.xml

最后在AndroidManifest.xml配上

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

android之写文件到sd卡的更多相关文章

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

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

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

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

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

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

  4. android保存文件到SD卡中

    想把文件保存到SD卡中,一定要知道SD卡的路径,有人说可以用File explore来查看,这种方法不太好,因为随着android版本的升级,SD卡的路径可能会发生改变.在1.6的时候SD的路径是/s ...

  5. 从网络上下载文件到sd卡上

    String SDPATH = Environment.getExternalStorageDirectory() + "/"; String path = SDPATH + &q ...

  6. 第四十五篇--将文件写入SD卡

    RAM: 运行内存 ROM: 外部存储,手机内部存储 SD卡:外部存储,SD卡存储. 在存储文件时千万不要忘记向清单文件中添加相应权限,并且android6.0以后还要添加运行时权限 还有一个权限有所 ...

  7. [android] 分析setting源代码获取SD卡大小

    保存文件到sd卡需要判断sd卡的大小,通过查看android系统的自带应用的源代码,得到方法,sdk下面的source是sdk的源代码,包含的是android.Jar下面的所有class的源代码.在a ...

  8. android学习笔记47——读写SD卡上的文件

    读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...

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

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

随机推荐

  1. 知识点1-2:ASP.NET MVC背景

    1.发展阶段 CGI(公共网关接口)-->ASP(Active Server Pages,活动服务器页面)-->.NET 2. .NET平台 2002年初,微软发布了第一版.NET框架,这 ...

  2. php实现加好友功能

    思路: 1用户发送好友申请之后 把申请储存到申请数据表中,状态为 未验证 2 当用户登录时,查询申请表中是否有uid和被申请人id相同的,如果同意,更改状态,并把数据插入到对应的好友数据表否则,删除申 ...

  3. Linux内核-系统调用

    Linux内核-系统调用 1.与内核通信 #系统调用在用户空间进程和硬件设备之间添加了一个中间层 作用:1.为用户空间提供了一种硬件的抽象接口 2.系统调用保证了系统的稳定和安全 3.出于每一个进程都 ...

  4. PHP - Windows安装Pear

    1. 打开命令窗口,切换到php的安装目录,执行以下命令(你也可以添加一个php的环境变量,就不用如此麻烦的切换目录,但是我安装了多个版本的php,所以就没有添加环境变量). 2. 当出现下面这句话时 ...

  5. python3.4 尝试 py2exe

    第一次成功将python3.4脚本生成 exe文件. 测试环境:win8.1 32位,python3.4,pyside py打包成exe的工具我所知道的有三种 cx-freeze , py2exe , ...

  6. Swift - 单例模式的实现

    过去Swift要实现单例,无非是这三种方式:全局变量,内部变量和dispatch_once方式.但都略显繁琐. 后来从1.2版本起,Swift中添加了如 static let 和 static var ...

  7. QT中的pro文件的编写

    原地址:http://blog.csdn.net/fjb2080/article/details/4833666 我们在编译QT的工程的时候,一般都会让qmake自动生成,但有时我们需要定制我们的工程 ...

  8. 俄罗斯方块SDK版

    前言 本来可以从俄罗斯方块控制台版改一版, 将UI接口换掉, 变成SDK版. 正好放假了, 有时间. 就用了一个星期来重头做一个新版, 享受一下静下心来, 有条不紊干活的感觉^_^ 这个工程用来验证S ...

  9. MySql截取DateTime字段的日期值

    用 DATE_FORMAT 来格式化日期字段 SELECT DATE_FORMAT(crt_time,'%Y-%m-%d') FROM ad_n_advertise_t

  10. ufldl学习笔记和编程作业:Feature Extraction Using Convolution,Pooling(卷积和汇集特征提取)

    ufldl学习笔记与编程作业:Feature Extraction Using Convolution,Pooling(卷积和池化抽取特征) ufldl出了新教程,感觉比之前的好,从基础讲起.系统清晰 ...