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. javascript每日一练(十一)——多物体运动

    一.多物体运动 需要注意:每个运动物体的定时器作为物体的属性独立出来互不影响,属性与运动对象绑定,不能公用: 例子1: <!doctype html> <html> <h ...

  2. Offer_1

    #include <iostream> #include <cstring> using namespace std; class CMyString { public: CM ...

  3. mysql 安装过程中的错误:my-template.ini could not be processed and written to XXX\my.ini.Error code-1

    安装mysql的过程中,在最后配置mysql时,提示错误:Configuration file tmeplate E:\编程\MySQL\my-template.ini could not be pr ...

  4. iphone分辨率终极指南(含有iphone6/6+)

    如文本不清楚.请 "对->图片另存为" 下载大图后, ------------------------- 原文及翻译文本 Points点 At the beginning, ...

  5. Jquery中的$().each,$.each的区别

    在jquery中,遍历对象和数组,经常会用到$().each和$.each(),两个方法.两个方法是有区别的,从而这两个方法在针对不同的操作上,显示了各自的特点. $().each,对于这个方法,在d ...

  6. Android SurfaceView实战 打造抽奖转盘

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/41722441 ,本文出自:[张鸿洋的博客] 1.概述 今天给大家带来Surfac ...

  7. [hadoop系列]Pig的安装和简单演示样例

    inkfish原创,请勿商业性质转载,转载请注明来源(http://blog.csdn.net/inkfish ).(来源:http://blog.csdn.net/inkfish) Pig是Yaho ...

  8. 免插件打造wordpress投稿页面

    一.新建投稿页面模板 把主题的 page.php 另存为 tougao.php,并且在第一行的 <?php 之后添加模板的标识注释: /* Template Name: tougao */ 紧接 ...

  9. 14.18.1 The InnoDB Recovery Process InnoDB 恢复进程:

    14.18.1 The InnoDB Recovery Process InnoDB 恢复进程: InnoDB crash recovery 有几个步骤组成: 1.应用redo log,Redo lo ...

  10. Windows8 Metro快捷键 | Win8迷

    Windows8 Metro快捷键 | Win8迷   Win + Q : 打开 搜索面板 Win + C : 打开屏幕右侧的Charms简化菜单 Win + 空格 : 切换输入语言和键盘布局