1 package com.kevin.writeorreadfile1_1; 2 3 import android.app.Activity; 4 import android.bluetooth.le.ScanFilter; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.media.MediaScannerConnection; 8 import android.os.Bundle; 9 import android.os.Environment; 10 import android.util.Log; 11 import android.view.View; 12 import android.widget.Button; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15 16 import java.io.BufferedInputStream; 17 import java.io.BufferedOutputStream; 18 import java.io.File; 19 import java.io.FileNotFoundException; 20 import java.io.FileOutputStream; 21 import java.io.IOException; 22 import java.io.InputStream; 23 import java.io.OutputStream; 24 import java.util.Scanner; 25 26 /** 27 * 保存并读取数据 28 * 1.确保外部SD卡可用Environment.getExternalStorageState(); 29 * 2.获取SD卡的根路径 30 * 3.写入权限 31 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 32 * <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> 33 * 4.读取 34 */ 35 public class MainActivity extends Activity implements View.OnClickListener { 36 private boolean state = true; 37 private File file; 38 private ImageView img; 39 private Button keepBtn,readBtn; 40 @Override 41 protected void onCreate(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 @Override 63 public void onClick(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 void saveImg(){ 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 = new FileOutputStream(file); 87 //缓存机制 88 is = new BufferedInputStream(inputStream); 89 os = new BufferedOutputStream(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 void ScanFile(String path) { 123 124 MediaScannerConnection.scanFile(this,new String[]{path},null,null); 125 } 126 127 //显示图片 128 public void showImg(){ 129 //从文件中读取出来 130 Bitmap bitmap = BitmapFactory.decodeFile(file.getPath()); 131 img.setImageBitmap(bitmap); 132 } 133 }
时间: 2024-11-09 04:26:08