读写SD卡上的文件
通过Context的openFileInput、openFileOutput来打开文件输入流、输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限——手机内存所限。
SD卡可以更好的存、取应用程序的大小文件数据。SD卡可以大大扩充手机的存储能力。
读、写SD卡上的文件的操作步骤如下:
1.调用Environment的getExternalStorageState()方法判断手机上是否插入了SD卡, 并且应用程序具有读写SD卡的权限。例如使用如下代码:
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
2.调用Environment的getExternalStorageDirectory()方法来获取外部存储器,也就是SD卡的目录;
3.使用FileInputStream、FileOutputStream、FileReader/FileWriter读、写SD卡里面的文件。
注意:
应用程序读、写SD卡的文件有如下两个注意点:
1.手机上应该已插入SD卡。对于模拟器来说,可通过mksdcard命令来创建虚拟存储卡。
2.为了读、写SD卡上的数据,必须在应用程序的清单文件(AndroidMainfest.xml)中添加读写SD卡的权限.
Eg:
实例如下:
布局文件==》
<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" >
<EditText
android:id="@+id/edit1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnWrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="write" />
<EditText
android:id="@+id/edit2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnRead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="read" />
</LinearLayout>
代码实现==》
package com.example.mysdcard;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity
{
final String FILE_NAME = "/crazyit";
EditText Edit1;
EditText Edit2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnWrite = (Button) this.findViewById(R.id.btnWrite);
Button btnRead = (Button) this.findViewById(R.id.btnRead);
Edit1 = (EditText) this.findViewById(R.id.edit1);
Edit2 = (EditText) this.findViewById(R.id.edit2);
btnWrite.setOnClickListener(new MyButtonClick());
btnRead.setOnClickListener(new MyButtonClick());
}
private class MyButtonClick implements OnClickListener
{
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnWrite:
Log.i("swg", "write==" + Edit1.getText());
write(Edit1.getText().toString());
Edit1.setText("");
break;
case R.id.btnRead:
Log.i("swg", "read==" + read());
Edit2.setText(read());
break;
}
}
private String read()
{
try
{
File file = new File(Environment.getExternalStorageDirectory(), FILE_NAME);
BufferedReader br = new BufferedReader(new FileReader(file));
String readline = "";
StringBuffer sb = new StringBuffer();
while ((readline = br.readLine()) != null)
{
System.out.println("readline:" + readline);
sb.append(readline);
}
br.close();
System.out.println("读取成功:" + sb.toString());
} catch (Exception e)
{
e.printStackTrace();
}
return null;
}
private void write(String context)
{
try
{
Log.i("swg", "write");
// 如果手机插入SD卡,且应用程序具有访问权限
boolean value =Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
Log.i("swg", "value==" + value);
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Log.i("swg", "sd exist ok");
// 获取SD卡对应的存储目录
File sdcardDir = Environment.getExternalStorageDirectory();
String filePath = sdcardDir.getCanonicalPath() + FILE_NAME;
Log.i("swg", filePath);
File targerFile = new File(filePath);
// 以拟定文件创建RandomAccessFile对象
RandomAccessFile raf = new RandomAccessFile(targerFile, "rw");
raf.seek(targerFile.length());
raf.write(context.getBytes());
raf.close();
} else
Log.i("swg", "sd is not op");
} catch (Exception e)
{
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}