package gongzibai.co.cc;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
public class SD1Activity extends
Activity {
EditText editText1;
private final String SD_PATH = android.os.Environment
.getExternalStorageDirectory()
.getAbsolutePath();
//设置文件夹
public static final String FILE_PATH = "/loveyou1";
private File myFilePath;
/** Called when the activity is first created. */
@Override
public void onCreate(
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
editText1 = (EditText) findViewById(R.id.editText1);
//不存在则创建
myFilePath = new File(SD_PATH
+ FILE_PATH);
if (!myFilePath.exists()) {
myFilePath.mkdirs();
}
final ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
findViewById(R.id.button1)
.setOnClickListener(
new OnClickListener() {
@Override
public void onClick(
View v) {
// TODO Auto-generated method stub
try {
//写入文件名,实例化变量
File file = new File(
SD_PATH
+ FILE_PATH
+ "/"
+ "abc.txt");
//将写入的文件定义成类
FileOutputStream fileOutputStream = new FileOutputStream(
file);
fileOutputStream
.write(editText1
.getText()
.toString()
.getBytes());
fileOutputStream
.close();
editText1
.setText("");
}
catch (Exception e) {
// TODO: handle exception
}
}
});
}
public boolean onKeyDown(
int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
try {
File file = new File(
SD_PATH
+ FILE_PATH
+ "/"
+ "abc.txt");
FileInputStream fileInputStream = new FileInputStream(
file);
int length = (int) file
.length();
byte[] message = new byte[length];
fileInputStream
.read(message);
fileInputStream.close();
String msg01 = "";
for (int i = 0; i < message.length; i++) {
char c = (char) message[i];
msg01 = msg01 + c;
}
editText1
.setText(msg01);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
} else {
return super.onKeyDown(
keyCode, event);
}
}
}