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. ArrayList集合-[习题]--C#

    :向集合中添加10个元素,计算平均值,求最大.最小值. ; list.AddRange(, , , , , , , , }); int Max, Min; Max = Min = (]; ; i &l ...

  2. 编绎OpenJDK

    因为对于Java里的vtable,itable,有个地方还没搞明白,不得已去下个OpenJDK来研究下. 本来很不愿意去编绎OpenJDK,因为很有可能做的只是无用功,还有可能要去解决各种找不到链接库 ...

  3. 14-UIKit(拖拽手势、布局)

    目录: 1.手势创建的拖拽方式 2.frame,bounds,transform,center区别 3.触控(touch) 4.布局 5.代码布局 回到顶部 1.手势创建的拖拽方式 创建手势对象,修改 ...

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

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

  5. Bootstrap技术: 如何给nav导航组件的tab页增加关闭按钮以及动态的添加和关闭tab页

    先给出示例html代码 <div> <!-- Nav tabs --> <ul class="nav nav-tabs" role="tab ...

  6. CCIE路由实验(6) -- 组播Multicasting

    1.组播IGMP的各种情况2.PIM Dense-Mode3.PIM Sparse-Mode4.PIM双向树和SSM5.动态RP之auto-rp6.动态RP之BSR7.Anycast RP8.域间组播 ...

  7. 在前端页面中使用@font-face来显示web自定义字体【转】

    本文转自W3CPLUS 的<CSS @font-face> @font-face是CSS3中的一个模块,他主要是把自己定义的Web字体嵌入到你的网页中,随着@font-face模块的出现, ...

  8. Demo XML 、 JSON 解析 AND 网络HTTP请求

    有道云笔记分享:http://note.youdao.com/share/?id=7950b949a5017a698a9ecc95bc250ec5&type=note 后台服务端:C#.服务器 ...

  9. VS2008+Qt 项目目录

    1.项目开发环境:VS2008,QT4.7 2.项目的目录: 1)PETCT是解决方案名字 2)Bin目录存放所有动态链接库和执行档,包括自己的产出和第三方库,区分Release和Debug两个版本. ...

  10. PHP中遍历stdclass object 及 json 总结[中国航天神舟十号以json形式向地面返回数据]

    $test=Array ( [0] => stdClass Object ( [tags] => 最快的车,Bloodhound,SSC [id] => 48326888 11 从网 ...