1. public class DataActivity extends Activity {
  2. private EditText filenameText;
  3. private EditText contentText;
  4. private TextView resultView;
  5. private static final String TAG = "DataActivity";
  6. /** Called when the activity is first created. */
  7. @Override
  8. public void onCreate(Bundle savedInstanceState) {
  9. super.onCreate(savedInstanceState);
  10. setContentView(R.layout.main);
  11. filenameText = (EditText) this.findViewById(R.id.filename);
  12. contentText = (EditText) this.findViewById(R.id.content);
  13. resultView = (TextView) this.findViewById(R.id.result);
  14. String filename = filenameText.getText().toString();
  15. Button button = (Button) this.findViewById(R.id.button);
  16. Button showButton = (Button) this.findViewById(R.id.showButton);
  17. button.setOnClickListener(listener);
  18. showButton.setOnClickListener(listener);
  19. }
  20.  
  21. private View.OnClickListener listener = new View.OnClickListener() {
  22. @Override
  23. public void onClick(View v) {
  24. Button button = (Button) v;
  25. String filename = filenameText.getText().toString();
  26. switch(button.getId()){
  27. case R.id.button://如果是保存按钮
  28. int resId = R.string.success;
  29.  
  30. String content = contentText.getText().toString();
  31. File file = new File(Environment.getExternalStorageDirectory(),filename);
  32. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  33. try {
  34. //OutputStream outStream = DataActivity.this.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE+Context.MODE_WORLD_READABLE);
  35. //四中操作模式
  36. //Context.MODE_PRIVATE=0 覆盖、私有
  37. //Context.MODE_APPEND=32768追加、私有
  38. //Context.MODE_WORLD_READABLE=1其他的程序可以访问
  39. //Context.MODE_WORLD_WRITEABLE=2
  40. //Environment.getExternalStorageDirectory()==new file("/sdcard")
  41.  
  42. FileOutputStream outStream = new FileOutputStream(file);
  43. try {
  44. FileService.save(outStream, content);
  45. } catch (Exception e) {
  46. Log.e(TAG, e.toString());
  47. resId = R.string.error;
  48. }
  49. } catch (FileNotFoundException e) {
  50. Log.e(TAG, e.toString());
  51. resId = R.string.error;
  52. }
  53. Toast.makeText(DataActivity.this, resId, Toast.LENGTH_LONG).show();
  54. }
  55. else{
  56. Toast.makeText(DataActivity.this,"SD卡不存在或者写保护", Toast.LENGTH_LONG).show();
  57. }
  58. break;
  59. case R.id.showButton://如果是显示按钮
  60. try {
  61. InputStream inStream = DataActivity.this.openFileInput(filename);
  62. String text = FileService.read(inStream);
  63. resultView.setText(text);
  64. } catch (Exception e) {
  65. Log.e(TAG, e.toString());
  66. resId = R.string.error;
  67. Toast.makeText(DataActivity.this, "读取失败", Toast.LENGTH_LONG).show();
  68. }
  69.  
  70. break;
  71. }
  72. }
  73. };
  74.  
  75. }

  

Android往SD卡上存储文件的更多相关文章

  1. android 操作SD卡上的文件

    (1)说明:操作SD卡上的文件须要增加下面权限  在SD卡上创建和删除文件权限  <uses-permission android:name="android.permission.M ...

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

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

  3. Android开发之SD卡上文件操作

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  4. Android权限安全(12)apk安装在sd卡上时,如何保证数据安全

    apk安装在sd卡上时,如果把sd卡拿下安在另一个手机B上,那么apk的数据就可以被B里的恶意应用访问了. 下面是android解决这个问题的方案: 绑定设备 1,绑定perDevice使得应用以及应 ...

  5. Android全屏截图的方法,返回Bitmap并且保存在SD卡上

    Android全屏截图的方法,返回Bitmap并且保存在SD卡上 今天做分享,需求是截图分享,做了也是一个运动类的产品,那好,我们就直接开始做,考虑了一下,因为是全屏的分享,所有很自然而然的想到了Vi ...

  6. Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上

    假设使用默认的系统管理,默认放在包以下.比較省心.并且在卸载app后不会造成数据残留.可是这样也有一个问题.比方我做一个背单词的软件,那么当用户卸载掉这个app时,他辛辛苦苦下载的单词库也没了... ...

  7. android中读取SD卡上的数据

    通过Context的openFileInput或者openFileOutput打开的文件输入输出流是操作应用程序的数据文件夹里的文件,这样存储的大小比较有限,为了更好的存取应用程序的大文件数据,应用程 ...

  8. Android SD卡上文件

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  9. Android读写SD卡

    SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...

随机推荐

  1. python数字图像处理(11):图像自动阈值分割

    图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素 ...

  2. [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 ...

  3. LeetCode:Gray Code(格雷码)

    题目链接 The gray code is a binary numeral system where two successive values differ in only one bit. Gi ...

  4. 蓝牙技术BlueTooth

    转载网址:http://blog.csdn.net/dxdxsmy/article/details/7790568 蓝牙核心架构概念的理解请参考上面的网址.

  5. iOS: 使用CGContextRef,CGPath和UIBezierPath来绘画

    这三种东西:CGContextRef,CGPath和UIBezierPath.本质上都是一样的,都是使用Quartz来绘画.只不过把绘图操作暴露在不同的API层面上,在具体实现上,当然也会有一些细小的 ...

  6. rem详解及使用方法

    好像有一段时间没有写博客了……今天刚好总结一下rem的使用方法 首先,先说一个常识,浏览器的默认字体高都是16px.步入正题-----〉 兼容性: 目前,IE9+,Firefox.Chrome.Saf ...

  7. Android中RelativeLayout属性详细说明

    android:layout_above="@id/xxx"  --将控件置于给定ID控件之上android:layout_below="@id/xxx"  - ...

  8. iOS 自定义控件开发(上)

    工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> #0 目 ...

  9. 第六章 prototype和constructor

    首先我们看下面一段代码(第六章 01.htm) function myfun() //定义一个函数myfun { }; console.log(typeof (myfun.prototype)); c ...

  10. 03.C#委托(二章1.1)

    一章1.5-1.8介绍的是com.动态类型及.NET平台一些说明,每个心中都有自己的标准,听一家之言,叫人不爽,相信自己有自己的标准和自己的编程理念就OK了,也不想码那么多说明性的文字,直接跳过吧,当 ...