android 操作SD卡上的文件
(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)布局文件
<?
xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/label_01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20dp" />
<EditText
android:id="@+id/filename"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignTop="@id/label_01"
android:layout_toRightOf="@id/label_01" />
</RelativeLayout>
<TextView
android:id="@+id/label_02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/neirong" />
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="120px"
android:ems="10" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/savebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/save" />
<Button
android:id="@+id/readbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="@string/read" />
</LinearLayout>
<TextView
android:id="@+id/textcontent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.10" />
</LinearLayout>
(3)代码
package com.liuzuyi.file;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends Activity {
private EditText filename;
private EditText context;
private TextView textcontent;
private static final String TAG="simplefile";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
filename=(EditText)this.findViewById(R.id.filename);
context=(EditText)this.findViewById(R.id.content);
textcontent=(TextView)this.findViewById(R.id.textcontent);
Button savebtn=(Button)this.findViewById(R.id.savebutton);
Button viewbtn=(Button)this.findViewById(R.id.readbutton);
savebtn.setOnClickListener(l);
viewbtn.setOnClickListener(l);
}
private View.OnClickListener l =new OnClickListener() {
public void onClick(View v) {
Button button =(Button)v;
//过滤掉开头结尾的空格
String namestr = filename.getText().toString().trim();
String contentstr =context.getText().toString();
switch ( button.getId() ) {
case R.id.savebutton:
String resid_s ="success";
OutputStream fileos = null;
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
FileOutputStream outstream = new FileOutputStream("/sdcard/"+namestr+".txt");
outstream.write(contentstr.getBytes() );
outstream.close();
}
else
return ;
} catch (Exception e) {
resid_s = "faile";
e.printStackTrace();
}
Toast.makeText(MainActivity.this, resid_s,Toast.LENGTH_LONG).show();
Log.i(TAG, namestr);
Log.i(TAG, contentstr);
break;
case R.id.readbutton:
String resid_v ="success";
InputStream filsIs= null;
String contentst = null;
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
File SDCardDir=Environment.getExternalStorageDirectory();
File saveFile = new File(SDCardDir,namestr+".txt");
FileInputStream instream = new FileInputStream(saveFile);
ByteArrayOutputStream Ostream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len = instream.read(buffer)) != -1 )
{
Ostream.write(buffer,0,len);
}
contentst = Ostream.toString();
Ostream.close();
instream.close();
}
else
{
Toast.makeText(MainActivity.this, "SD卡不存在", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
resid_v ="faile";
e.printStackTrace();
}
textcontent.setText( contentst);
Log.i(TAG, contentst);
Toast.makeText(MainActivity.this, resid_v,Toast.LENGTH_LONG).show();
Log.i(TAG,namestr);
break;
}
}
};
}
android 操作SD卡上的文件的更多相关文章
- android学习笔记47——读写SD卡上的文件
读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...
- android 往sd卡中写入文件
在调用前需要判断是否有写入权限 Environment类提供了比较丰富的方法 static File getDataDirectory() 获得android data的目录. static File ...
- Android往SD卡上存储文件
public class DataActivity extends Activity { private EditText filenameText; private EditText content ...
- Android扫描SD卡中的文件
当android的系统启动的时候,系统会自动扫描sdcard内的多媒体文件,并把获得的信息保存在一个系统数据库中,以后在其他程序中如果想要访问多媒体文件的信息,其实就是在这个数据库中进行的,而不是直接 ...
- Android开发之SD卡上文件操作
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- Android SD卡上文件
1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...
- android中读取SD卡上的数据
通过Context的openFileInput或者openFileOutput打开的文件输入输出流是操作应用程序的数据文件夹里的文件,这样存储的大小比较有限,为了更好的存取应用程序的大文件数据,应用程 ...
- Android 读写SD卡的文件
今天介绍一下Android 读写SD卡的文件,要读写SD卡上的文件,首先需要判断是否存在SD卡,方法: Environment.getExternalStorageState().equals(Env ...
- Android读写SD卡
SD卡的读写是我们在开发Android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String sdCardRoot = Environment.getEx ...
随机推荐
- javascript的初步认识
把握自己,就是时时拥有一颗清澈的心,拥有一片明朗的情怀.嘿嘿,我们在2014-2015的跨度里,我们休息了的四天,今天又回到了学习的阶段,敲起来键盘突然有点陌生,想一想时间真的好快,在这里我们已经是跨 ...
- Python pyQt4/pyQt5 学习笔记1(空白窗口,按钮,控件事件,控件提示,窗体显示到屏幕中间,messagebox)
PyQt4是用来编写有图形界面程序(GUI applications)的一个工具包.PyQt4作为一个Python模块来使用,它有440个类和超过6000种函数和方法.同时它也是一个可以在几乎所有主流 ...
- 题目1100:最短路径(最短路径问题进阶dijkstra算法)
题目链接:http://ac.jobdu.com/problem.php?pid=1100 详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- isolinux.cfg 文件是干什么的
1. 首先光盘镜像也就是iso文件采用的是“ISO 9660 ”文件系统 . cd上的文件都存在这个简单的iso文件系统里,linux可以用mount -o loop 直接把*.iso文件mou ...
- C语言程序设计--字符串与指针及数组与指针
数组的基本知识 数组的定义 #define SIZE 5 int array_int[5]; //未声明初始化,默认填零 float array_float[5] = {1.01, 2.23, 3.1 ...
- Android开发之ActionBar
使用微信APP的小伙伴对于微信的ActionBar一定有印象,今天就带领大家一起实现以下这个效果. 第一步打开我们的开发工具,这里我使用的是Eclipse+ADT插件,然后创建我们的工程,这里选择An ...
- CreateTimerQueueTimer在DllMain中调用导致的loader lock
开发一个COM组件在Windows 7上注册成功,但是Windows XP SP3版本却导致regsvr32.exe进程挂起.用WinDbg查看发现提示: Break- seconds... WARN ...
- FileInputStream 和 FileOutputStream
简介 FileInputStream和FileOutputStream都是用来处理二进制数据源磁盘文件的流的. 他们分别派生自顶层抽象类InputStream和OutputStream FileInp ...
- centos6.8升级python3.5.2
1.查看系统python版本 [root@myserver01 Python-]# python -V Python 2.升级3.5.2 A.下载:wget https://www.python.or ...
- vue---进行post和get请求
参考文档: https://www.jb51.net/article/125717.htm 使用axios <script src="https://unpkg.com/axios/d ...