实验思路:先定义两个Buttton,分别进行写入数据和读取数据的事件处理,读取数据的时候,要先判断数据是否已存入sd卡中,然后再获取sd卡中的内容,弹出Toast,证明数据已存入sd卡中。注意在注册文件中要获取到相应的权限。
一、效果图:
二、主要代码:
(1)MainActivity.java
实现向sd卡中写入数据和从sd卡中读取数据的功能:
package com.example.filesd;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button button1;
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) this.findViewById(R.id.button1);
button2 = (Button) this.findViewById(R.id.button2);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
writeSDcard("Hello,恭喜您数据读取了!");
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
readSDcard();
}
});
}
// 向SD卡写入数据
private void writeSDcard(String str) {
try {
// 判断是否存在SD卡
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取SD卡的目录
File sdDire = Environment.getExternalStorageDirectory();
FileOutputStream outFileStream = new FileOutputStream(
sdDire.getCanonicalPath() + "/test.txt");
outFileStream.write(str.getBytes());
outFileStream.close();
Toast.makeText(this, "数据保存到text.txt文件了", Toast.LENGTH_LONG)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 从SD卡中读取数据
private void readSDcard() {
StringBuffer strsBuffer = new StringBuffer();
try {
// 判断是否存在SD
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory()
.getCanonicalPath() + "/test.txt");
// 判断是否存在该文件
if (file.exists()) {
// 打开文件输入流
FileInputStream fileR = new FileInputStream(file);
BufferedReader reads = new BufferedReader(
new InputStreamReader(fileR));
String st = null;
while ((st = reads.readLine()) != null) {
strsBuffer.append(st);
}
fileR.close();
} else {
Toast.makeText(this, "该目录下文件不存在", Toast.LENGTH_LONG).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(this, "读取到的数据是:" + strsBuffer.toString() + "",
Toast.LENGTH_LONG).show();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="向SD中写入数据" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="从SD卡中读取数据" />
</LinearLayout>
(3)AndroidManifest.xml(注册文件代码)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.filesd"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 从SDCard读取数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.filesd.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>