Android往SD卡上存储文件
- public class DataActivity extends Activity {
- private EditText filenameText;
- private EditText contentText;
- private TextView resultView;
- private static final String TAG = "DataActivity";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- filenameText = (EditText) this.findViewById(R.id.filename);
- contentText = (EditText) this.findViewById(R.id.content);
- resultView = (TextView) this.findViewById(R.id.result);
- String filename = filenameText.getText().toString();
- Button button = (Button) this.findViewById(R.id.button);
- Button showButton = (Button) this.findViewById(R.id.showButton);
- button.setOnClickListener(listener);
- showButton.setOnClickListener(listener);
- }
- private View.OnClickListener listener = new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- Button button = (Button) v;
- String filename = filenameText.getText().toString();
- switch(button.getId()){
- case R.id.button://如果是保存按钮
- int resId = R.string.success;
- String content = contentText.getText().toString();
- File file = new File(Environment.getExternalStorageDirectory(),filename);
- if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
- try {
- //OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
- //四中操作模式
- //Context.MODE_PRIVATE=0 覆盖、私有
- //Context.MODE_APPEND=32768追加、私有
- //Context.MODE_WORLD_READABLE=1其他的程序可以访问
- //Context.MODE_WORLD_WRITEABLE=2
- //Environment.getExternalStorageDirectory()==new file("/sdcard")
- FileOutputStream outStream = new FileOutputStream(file);
- try {
- FileService.save(outStream, content);
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- resId = R.string.error;
- }
- } catch (FileNotFoundException e) {
- Log.e(TAG, e.toString());
- resId = R.string.error;
- }
- Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(DataActivity.this,"SD卡不存在或者写保护", Toast.LENGTH_LONG).show();
- }
- break;
- case R.id.showButton://如果是显示按钮
- try {
- InputStream inStream = DataActivity.this.openFileInput(filename);
- String text = FileService.read(inStream);
- resultView.setText(text);
- } catch (Exception e) {
- Log.e(TAG, e.toString());
- resId = R.string.error;
- Toast.makeText(DataActivity.this, "读取失败", Toast.LENGTH_LONG).show();
- }
- break;
- }
- }
- };
- }
Android往SD卡上存储文件的更多相关文章
- android 操作SD卡上的文件
(1)说明:操作SD卡上的文件须要增加下面权限 在SD卡上创建和删除文件权限 <uses-permission android:name="android.permission.M ...
- android学习笔记47——读写SD卡上的文件
读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...
- Android开发之SD卡上文件操作
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- Android权限安全(12)apk安装在sd卡上时,如何保证数据安全
apk安装在sd卡上时,如果把sd卡拿下安在另一个手机B上,那么apk的数据就可以被B里的恶意应用访问了. 下面是android解决这个问题的方案: 绑定设备 1,绑定perDevice使得应用以及应 ...
- Android全屏截图的方法,返回Bitmap并且保存在SD卡上
Android全屏截图的方法,返回Bitmap并且保存在SD卡上 今天做分享,需求是截图分享,做了也是一个运动类的产品,那好,我们就直接开始做,考虑了一下,因为是全屏的分享,所有很自然而然的想到了Vi ...
- Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上
假设使用默认的系统管理,默认放在包以下.比較省心.并且在卸载app后不会造成数据残留.可是这样也有一个问题.比方我做一个背单词的软件,那么当用户卸载掉这个app时,他辛辛苦苦下载的单词库也没了... ...
- android中读取SD卡上的数据
通过Context的openFileInput或者openFileOutput打开的文件输入输出流是操作应用程序的数据文件夹里的文件,这样存储的大小比较有限,为了更好的存取应用程序的大文件数据,应用程 ...
- Android SD卡上文件
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- Android读写SD卡
SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...
随机推荐
- python数字图像处理(11):图像自动阈值分割
图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素 ...
- [CareerCup] 7.5 A Line Cut Two Squares in Half 平均分割两个正方形的直线
7.5 Given two squares on a two-dimensional plane, find a line that would cut these two squares in ha ...
- LeetCode:Gray Code(格雷码)
题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...
- 蓝牙技术BlueTooth
转载网址:http://blog.csdn.net/dxdxsmy/article/details/7790568 蓝牙核心架构概念的理解请参考上面的网址.
- iOS: 使用CGContextRef,CGPath和UIBezierPath来绘画
这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘画.只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的 ...
- rem详解及使用方法
好像有一段时间没有写博客了……今天刚好总结一下rem的使用方法 首先,先说一个常识,浏览器的默认字体高都是16px.步入正题-----〉 兼容性: 目前,IE9+,Firefox.Chrome.Saf ...
- Android中RelativeLayout属性详细说明
android:layout_above="@id/xxx" --将控件置于给定ID控件之上android:layout_below="@id/xxx" - ...
- iOS 自定义控件开发(上)
工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> #0 目 ...
- 第六章 prototype和constructor
首先我们看下面一段代码(第六章 01.htm) function myfun() //定义一个函数myfun { }; console.log(typeof (myfun.prototype)); c ...
- 03.C#委托(二章1.1)
一章1.5-1.8介绍的是com.动态类型及.NET平台一些说明,每个心中都有自己的标准,听一家之言,叫人不爽,相信自己有自己的标准和自己的编程理念就OK了,也不想码那么多说明性的文字,直接跳过吧,当 ...