android之写文件到sd卡
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卡的更多相关文章
- android 写文件到sd卡问题小记
android 写文件到sd卡问题小记 事情是这样子的.... 这天我开始编写项目调试工具,高大上不?-----其实就是记录实时网络请求和崩溃日志相关等的小工具(此处一个会心的微笑). 然后我是这样写 ...
- cygwin下烧写文件到sd卡中
在cygwin下将firmware_sdcard.bin写入到sd卡中(cygwin需要以管理员身份启动) 1查看sd分区情况 cat /proc/partitions (为了找到sd卡的标记) 2 ...
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
1.android下junit测试框架配置 单元测试需要在手机中进行安装测试 (1).在清单文件中manifest节点下配置如下节点 <instrumentation android:name= ...
- android保存文件到SD卡中
想把文件保存到SD卡中,一定要知道SD卡的路径,有人说可以用File explore来查看,这种方法不太好,因为随着android版本的升级,SD卡的路径可能会发生改变.在1.6的时候SD的路径是/s ...
- 从网络上下载文件到sd卡上
String SDPATH = Environment.getExternalStorageDirectory() + "/"; String path = SDPATH + &q ...
- 第四十五篇--将文件写入SD卡
RAM: 运行内存 ROM: 外部存储,手机内部存储 SD卡:外部存储,SD卡存储. 在存储文件时千万不要忘记向清单文件中添加相应权限,并且android6.0以后还要添加运行时权限 还有一个权限有所 ...
- [android] 分析setting源代码获取SD卡大小
保存文件到sd卡需要判断sd卡的大小,通过查看android系统的自带应用的源代码,得到方法,sdk下面的source是sdk的源代码,包含的是android.Jar下面的所有class的源代码.在a ...
- android学习笔记47——读写SD卡上的文件
读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...
- [android] 保存文件到SD卡
/****************2016年5月4日 更新*****************************/ 知乎:为什么很多Android应用要把文件写到/sdcard目录下而不是写到/d ...
随机推荐
- typeof 使用介绍
JS中的变量是松散类型(即弱类型)的,可以用来保存任何类型的数据. typeof 可以用来检测给定变量的数据类型,可能的返回值:1. 'undefined' --- 这个值未定义: 2. 'boole ...
- 02-Foundation-NSMutableString、NSNumber、NSValue、NSDate、NSArray
目录: 一.NSMutableString可变字符串 二.NSNumber数字对象 三.NSValue 四.NSDate日期对象 五.NSArray数组对象 回到顶部 一.NSMutableStrin ...
- Qt 智能指针学习(7种QT智能指针和4种std智能指针)
从内存泄露开始? 很简单的入门程序,应该比较熟悉吧 ^_^ #include <QApplication> #include <QLabel> int main(int arg ...
- Android 设备管理器 阻止用户取消激活
该方案测试可行,系统版本4.4.2.它算是借助android系统的一个bug,不确定在后续更高的版本中是否修复. 该功能和360防卸载功能一样的实现原理. 主要的参考资料是:http://bbs.pe ...
- svn回滚版本2
svn 版本回滚 取消对代码的修改分为两种情况: 第一种情况:改动没有被提交(commit). 这种情况下,使用svn revert就能取消之前的修改. svn revert用法如下: # svn ...
- WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序)
原文:WCF技术剖析之二十七: 如何将一个服务发布成WSDL[基于WS-MEX的实现](提供模拟程序) 通过<如何将一个服务发布成WSDL[编程篇]>的介绍我们知道了如何可以通过编程或者配 ...
- char、signed char 和 unsigned char 的区别
ANSI C 提供了3种字符类型,分别是char.signed char.unsigned char.而不是像short.int一样只有两种(int默认就是signed int). 三者都占1个字节( ...
- hdu1151Air Raid
Air Raid Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HttpURLConnection中使用代理(Proxy)及其验证(Authentication)
HttpURLConnection中使用代理(Proxy)及其验证(Authentication) 使用Java的HttpURLConnection类可以实现HttpClient的功能,而不需要依赖任 ...
- Codeforces Round #112 (Div. 2)---A. Supercentral Point
Supercentral Point time limit per test 2 seconds memory limit per test 256 megabytes input standard ...