鸟语天空
保存文件到外部存储器(SD卡)
post by:追风剑情 2015-8-13 20:58

在AndroidManifest.xml中添加权限

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

视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please enter some text" />
    
    <EditText 
        android:id="@+id/txtText1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    <Button 
        android:id="@+id/btnSave"
        android:text="Save"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickSave" />

    <Button 
        android:id="@+id/btnLoad"
        android:text="Load"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClickLoad" />
</LinearLayout>

代码

package com.example.androidtest;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class FilesActivity1 extends Activity {

	EditText textBox;
	static final int READ_BLOCK_SIZE = 100;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_files);
		
		textBox = (EditText)findViewById(R.id.txtText1);
	}
	
	public void onClickSave(View view)
	{
		String str = textBox.getText().toString();
		try {
			//---SD Card Storage---
			File sdCard = Environment.getExternalStorageDirectory();//获取外部存储器的完整路径
			File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
			directory.mkdirs();
			File file = new File(directory, "textfile.txt");
			FileOutputStream fOut = new FileOutputStream(file);
			OutputStreamWriter osw = new OutputStreamWriter(fOut);
			osw.write(str);
			osw.flush();
			osw.close();
			
			Toast.makeText(getBaseContext(), "File saved successfully!", Toast.LENGTH_SHORT).show();
			textBox.setText("");
		} catch (IOException e) {
			Log.d("AndroidTest", e.getLocalizedMessage());
		}
	}
	
	public void onClickLoad(View view)
	{
		try {
			File sdCard = Environment.getExternalStorageDirectory();
			File directory = new File(sdCard.getAbsolutePath() + "/MyFiles");
			File file = new File(directory, "textfile.txt");
			FileInputStream fIn = new FileInputStream(file);
			InputStreamReader isr = new InputStreamReader(fIn);
			
			char[] inputBuffer = new char[READ_BLOCK_SIZE];
			String s = "";
			
			int charRead;
			while((charRead = isr.read(inputBuffer))>0){
				String readString = String.copyValueOf(inputBuffer, 0, charRead);
				s += readString;
				
				inputBuffer = new char[READ_BLOCK_SIZE]; 
			}
			
			textBox.setText(s);
			
			Toast.makeText(getBaseContext(), "File loaded successfully!", Toast.LENGTH_SHORT).show();
		} catch (IOException e) {
			Log.d("AndroidTest", e.getLocalizedMessage());
		}
	}
}

运行效果

sssssssssss.pnglllllllll.png

ddddddddddd.png

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容