(1)说明:操作SD卡上的文件须要增加下面权限

 在SD卡上创建和删除文件权限

 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

在SD卡上写入数据的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

在sd卡上的文件操作和我上一篇文章几乎相同,假设有不懂的能够看我的上一篇文章

 

android 读写文件演示样例

http://blog.csdn.net/liuzuyi200/article/details/25049183

仅仅只是多加了推断SD卡是否存在的方法

Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)

(2)布局文件

  1. <?
  2. xml version="1.0" encoding="utf-8"?>
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. android:orientation="vertical" >
  7. <RelativeLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:orientation="vertical" >
  11. <TextView
  12. android:id="@+id/label_01"
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:text="@string/name"
  16. android:textSize="20dp" />
  17. <EditText
  18. android:id="@+id/filename"
  19. android:layout_width="160dp"
  20. android:layout_height="wrap_content"
  21. android:layout_alignTop="@id/label_01"
  22. android:layout_toRightOf="@id/label_01" />
  23. </RelativeLayout>
  24. <TextView
  25. android:id="@+id/label_02"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="@string/neirong" />
  29. <EditText
  30. android:id="@+id/content"
  31. android:layout_width="fill_parent"
  32. android:layout_height="120px"
  33. android:ems="10" >
  34. <requestFocus />
  35. </EditText>
  36. <LinearLayout
  37. android:layout_width="fill_parent"
  38. android:layout_height="wrap_content"
  39. android:orientation="horizontal" >
  40. <Button
  41. android:id="@+id/savebutton"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content"
  44. android:gravity="right"
  45. android:text="@string/save" />
  46. <Button
  47. android:id="@+id/readbutton"
  48. android:layout_width="wrap_content"
  49. android:layout_height="wrap_content"
  50. android:gravity="right"
  51. android:text="@string/read" />
  52. </LinearLayout>
  53. <TextView
  54. android:id="@+id/textcontent"
  55. android:layout_width="match_parent"
  56. android:layout_height="wrap_content"
  57. android:layout_weight="0.10" />
  58. </LinearLayout>

(3)代码

  1. package com.liuzuyi.file;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. import android.support.v7.app.ActionBarActivity;
  9. import android.support.v7.app.ActionBar;
  10. import android.support.v4.app.Fragment;
  11. import android.app.Activity;
  12. import android.content.Context;
  13. import android.os.Bundle;
  14. import android.os.Environment;
  15. import android.util.Log;
  16. import android.view.LayoutInflater;
  17. import android.view.Menu;
  18. import android.view.MenuItem;
  19. import android.view.View;
  20. import android.view.View.OnClickListener;
  21. import android.view.ViewGroup;
  22. import android.widget.Button;
  23. import android.widget.EditText;
  24. import android.widget.TextView;
  25. import android.widget.Toast;
  26. import android.os.Build;
  27. public class MainActivity extends Activity {
  28. private EditText filename;
  29. private EditText context;
  30. private TextView textcontent;
  31. private static final String TAG="simplefile";
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. filename=(EditText)this.findViewById(R.id.filename);
  36. context=(EditText)this.findViewById(R.id.content);
  37. textcontent=(TextView)this.findViewById(R.id.textcontent);
  38. Button savebtn=(Button)this.findViewById(R.id.savebutton);
  39. Button viewbtn=(Button)this.findViewById(R.id.readbutton);
  40. savebtn.setOnClickListener(l);
  41. viewbtn.setOnClickListener(l);
  42. }
  43. private View.OnClickListener l =new OnClickListener() {
  44. public void onClick(View v) {
  45. Button button =(Button)v;
  46. //过滤掉开头结尾的空格
  47. String namestr = filename.getText().toString().trim();
  48. String contentstr =context.getText().toString();
  49. switch ( button.getId() ) {
  50. case R.id.savebutton:
  51. String resid_s ="success";
  52. OutputStream fileos = null;
  53. try {
  54. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
  55. {
  56. FileOutputStream outstream = new FileOutputStream("/sdcard/"+namestr+".txt");
  57. outstream.write(contentstr.getBytes() );
  58. outstream.close();
  59. }
  60. else
  61. return ;
  62. } catch (Exception e) {
  63. resid_s = "faile";
  64. e.printStackTrace();
  65. }
  66. Toast.makeText(MainActivity.this, resid_s,Toast.LENGTH_LONG).show();
  67. Log.i(TAG, namestr);
  68. Log.i(TAG, contentstr);
  69. break;
  70. case R.id.readbutton:
  71. String resid_v ="success";
  72. InputStream filsIs= null;
  73. String contentst = null;
  74. try {
  75. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
  76. {
  77. File SDCardDir=Environment.getExternalStorageDirectory();
  78. File saveFile = new File(SDCardDir,namestr+".txt");
  79. FileInputStream instream = new FileInputStream(saveFile);
  80. ByteArrayOutputStream Ostream = new ByteArrayOutputStream();
  81. byte[] buffer = new byte[1024];
  82. int len = -1;
  83. while( (len = instream.read(buffer)) != -1 )
  84. {
  85. Ostream.write(buffer,0,len);
  86. }
  87. contentst = Ostream.toString();
  88. Ostream.close();
  89. instream.close();
  90. }
  91. else
  92. {
  93. Toast.makeText(MainActivity.this, "SD卡不存在", Toast.LENGTH_LONG).show();
  94. }
  95. } catch (Exception e) {
  96. resid_v ="faile";
  97. e.printStackTrace();
  98. }
  99. textcontent.setText( contentst);
  100. Log.i(TAG, contentst);
  101. Toast.makeText(MainActivity.this, resid_v,Toast.LENGTH_LONG).show();
  102. Log.i(TAG,namestr);
  103. break;
  104. }
  105. }
  106. };
  107. }

android 操作SD卡上的文件的更多相关文章

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

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

  2. android 往sd卡中写入文件

    在调用前需要判断是否有写入权限 Environment类提供了比较丰富的方法 static File getDataDirectory() 获得android data的目录. static File ...

  3. Android往SD卡上存储文件

    public class DataActivity extends Activity { private EditText filenameText; private EditText content ...

  4. Android扫描SD卡中的文件

    当android的系统启动的时候,系统会自动扫描sdcard内的多媒体文件,并把获得的信息保存在一个系统数据库中,以后在其他程序中如果想要访问多媒体文件的信息,其实就是在这个数据库中进行的,而不是直接 ...

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

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

  6. Android SD卡上文件

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

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

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

  8. Android 读写SD卡的文件

    今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...

  9. Android读写SD卡

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

随机推荐

  1. javascript的初步认识

    把握自己,就是时时拥有一颗清澈的心,拥有一片明朗的情怀.嘿嘿,我们在2014-2015的跨度里,我们休息了的四天,今天又回到了学习的阶段,敲起来键盘突然有点陌生,想一想时间真的好快,在这里我们已经是跨 ...

  2. Python pyQt4/pyQt5 学习笔记1(空白窗口,按钮,控件事件,控件提示,窗体显示到屏幕中间,messagebox)

    PyQt4是用来编写有图形界面程序(GUI applications)的一个工具包.PyQt4作为一个Python模块来使用,它有440个类和超过6000种函数和方法.同时它也是一个可以在几乎所有主流 ...

  3. 题目1100:最短路径(最短路径问题进阶dijkstra算法)

    题目链接:http://ac.jobdu.com/problem.php?pid=1100 详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  4. isolinux.cfg 文件是干什么的

    1.   首先光盘镜像也就是iso文件采用的是“ISO 9660 ”文件系统 . cd上的文件都存在这个简单的iso文件系统里,linux可以用mount  -o loop 直接把*.iso文件mou ...

  5. C语言程序设计--字符串与指针及数组与指针

    数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...

  6. Android开发之ActionBar

    使用微信APP的小伙伴对于微信的ActionBar一定有印象,今天就带领大家一起实现以下这个效果. 第一步打开我们的开发工具,这里我使用的是Eclipse+ADT插件,然后创建我们的工程,这里选择An ...

  7. CreateTimerQueueTimer在DllMain中调用导致的loader lock

    开发一个COM组件在Windows 7上注册成功,但是Windows XP SP3版本却导致regsvr32.exe进程挂起.用WinDbg查看发现提示: Break- seconds... WARN ...

  8. FileInputStream 和 FileOutputStream

    简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...

  9. centos6.8升级python3.5.2

    1.查看系统python版本 [root@myserver01 Python-]# python -V Python 2.升级3.5.2 A.下载:wget https://www.python.or ...

  10. vue---进行post和get请求

    参考文档: https://www.jb51.net/article/125717.htm 使用axios <script src="https://unpkg.com/axios/d ...