java media_unmount file_Android外部SD卡的读取

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 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值