---------------------写入手机内存-------------------------
EditText et11=null;
Button btn11=null;
EditText et22=null;
Button btn22=null;
private final String fileName = "temp.txt";
private FileOutputStream fos = null;
private FileInputStream fis = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll= (LinearLayout) this.getLayoutInflater().inflate(R.layout.main1, null);
et11= (EditText) ll.findViewById(R.id.et011);
btn11= (Button) ll.findViewById(R.id.btn011);
et22= (EditText) ll.findViewById(R.id.et022);
btn22= (Button) ll.findViewById(R.id.btn022);
btn11.setText("write");
btn22.setText("read");
btn11.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
fos = WriteOrReadActivity.this.openFileOutput(fileName, Context.MODE_APPEND);
String userInput = et11.getText().toString();
byte[] text = userInput.getBytes();
fos.write(text);
} catch (Exception e) {
e.printStackTrace();
}finally{
if (fos != null){
try {
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
btn22.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
fis = WriteOrReadActivity.this.openFileInput(fileName);
//文件中没有数据
if (fis.available() == 0) {
return;
}
byte[] readBytes = new byte[fis.available()];
while (fis.read(readBytes) != -1) {
}
String userData = new String(readBytes);
et22.setText(userData);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
setContentView(ll);
---------------------读取手机内存-------------------------
EditText et=null;
Button btn=null;
private final static String fileName = "temp_sms";
private final static int fileMode = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll= (LinearLayout) this.getLayoutInflater().inflate(R.layout.main, null);
et= (EditText) ll.findViewById(R.id.et01);
btn= (Button) ll.findViewById(R.id.btn01);
btn.setText("send");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp= fileActivity.this.getSharedPreferences (fileName, fileMode);
String userInput = et.getText().toString();
SharedPreferences.Editor editor = sp.edit();
editor.putString("username", userInput);
editor.commit();
}
});
setContentView(ll);
}
---------------------读取手机内存1-------------------------
TextView tv=null;
ImageView iv=null;
Button btn=null;
InputStream is=null;
String imgPath="/data/data/com.android/afs.jpg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) this.findViewById(R.id.tv01);
iv=(ImageView) this.findViewById(R.id.iv01);
btn=(Button) this.findViewById(R.id.btn01);
Drawable d= this.getResources().getDrawable(R.drawable.practice20_1);
iv.setImageDrawable(d);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
is=new FileInputStream(imgPath);
Bitmap b= BitmapFactory.decodeStream(is);
iv.setImageBitmap(b);
tv.setText(imgPath);
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
//把byte 转成 Bitmap
private Bitmap getBitmap(byte[] b){
if(b!=null){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}else{
return null;
}
}
}