1 packagecom.kevin.writeorreadfile1_1;2
3 importandroid.app.Activity;4 importandroid.bluetooth.le.ScanFilter;5 importandroid.graphics.Bitmap;6 importandroid.graphics.BitmapFactory;7 importandroid.media.MediaScannerConnection;8 importandroid.os.Bundle;9 importandroid.os.Environment;10 importandroid.util.Log;11 importandroid.view.View;12 importandroid.widget.Button;13 importandroid.widget.ImageView;14 importandroid.widget.Toast;15
16 importjava.io.BufferedInputStream;17 importjava.io.BufferedOutputStream;18 importjava.io.File;19 importjava.io.FileNotFoundException;20 importjava.io.FileOutputStream;21 importjava.io.IOException;22 importjava.io.InputStream;23 importjava.io.OutputStream;24 importjava.util.Scanner;25
26 /**
27 * 保存并读取数据28 * 1.确保外部SD卡可用Environment.getExternalStorageState();29 * 2.获取SD卡的根路径30 * 3.写入权限31 * 32 * 33 * 4.读取34 */
35 public class MainActivity extends Activity implementsView.OnClickListener {36 private boolean state = true;37 privateFile file;38 privateImageView img;39 privateButton keepBtn,readBtn;40 @Override41 protected voidonCreate(Bundle savedInstanceState) {42 super.onCreate(savedInstanceState);43 setContentView(R.layout.activity_main);44 //初始化控件
45 keepBtn =(Button) findViewById(R.id.btn_keep_file);46 readBtn =(Button) findViewById(R.id.btn_read_file);47 img =(ImageView) findViewById(R.id.out_img);48
49 //点击事件
50 keepBtn.setOnClickListener(this);51 readBtn.setOnClickListener(this);52
53 //确保SD卡可用
54 String storageState = Environment.getExternalStorageState();//获取SD卡的状态
55 if(storageState.equals(Environment.MEDIA_MOUNTED)){56 Log.i("TAG","当前SD卡可用");57 }else{58 state = false;59 }60 }61
62 @Override63 public voidonClick(View view) {64 switch(view.getId()){65 case R.id.btn_keep_file://点击保存
66 if(state){67 saveImg();68 }69 break;70 case R.id.btn_read_file://点击读取
71 if(state){72 showImg();73 }74 break;75 }76 }77 //78 public voidsaveImg(){79 BufferedInputStream is = null;80 BufferedOutputStream os = null;81 try{82 File storageDirectory =Environment.getExternalStorageDirectory();83 file = new File(storageDirectory,"YangKai.jpg");84 //读取项目中的图片
85 InputStream inputStream =getResources().openRawResource(R.drawable.yangkai_photo);86 OutputStream outputStream = newFileOutputStream(file);87 //缓存机制
88 is = newBufferedInputStream(inputStream);89 os = newBufferedOutputStream(outputStream);90 //读写操作
91 int length = 0;92 byte[] bytes = new byte[1024];93 while ((length = is.read(bytes)) != -1){94 os.write(bytes,0,length);95 os.flush();//刷新操作
96 }97 Toast.makeText(this,"图片已保存",Toast.LENGTH_SHORT).show();98 ScanFile(file.getPath());99 } catch(FileNotFoundException e) {100 e.printStackTrace();101 } catch(IOException e) {102 e.printStackTrace();103 }finally{104 if ( is != null){105 try{106 is.close();107 } catch(IOException e) {108 e.printStackTrace();109 }110 }111 if (os != null){112 try{113 os.close();114 } catch(IOException e) {115 e.printStackTrace();116 }117 }118 }119 }120
121 //扫描进入图片
122 private voidScanFile(String path) {123
124 MediaScannerConnection.scanFile(this,new String[]{path},null,null);125 }126
127 //显示图片
128 public voidshowImg(){129 //从文件中读取出来
130 Bitmap bitmap =BitmapFactory.decodeFile(file.getPath());131 img.setImageBitmap(bitmap);132 }133 }