package com.kevin.writeorreadfile1_1;
import android.app.Activity;
import android.bluetooth.le.ScanFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
/**
* 保存并读取数据
* 1.确保外部SD卡可用Environment.getExternalStorageState();
* 2.获取SD卡的根路径
* 3.写入权限
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
* <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
* 4.读取
*/
public class MainActivity extends Activity implements View.OnClickListener {
private boolean state = true;
private File file;
private ImageView img;
private Button keepBtn,readBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
keepBtn = (Button) findViewById(R.id.btn_keep_file);
readBtn = (Button) findViewById(R.id.btn_read_file);
img = (ImageView) findViewById(R.id.out_img);
//点击事件
keepBtn.setOnClickListener(this);
readBtn.setOnClickListener(this);
//确保SD卡可用
String storageState = Environment.getExternalStorageState();//获取SD卡的状态
if(storageState.equals(Environment.MEDIA_MOUNTED)){
Log.i("TAG","当前SD卡可用");
}else {
state = false;
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_keep_file://点击保存
if(state){
saveImg();
}
break;
case R.id.btn_read_file://点击读取
if(state){
showImg();
}
break;
}
}
//
public void saveImg(){
BufferedInputStream is = null;
BufferedOutputStream os = null;
try {
File storageDirectory = Environment.getExternalStorageDirectory();
file = new File(storageDirectory,"YangKai.jpg");
//读取项目中的图片
InputStream inputStream = getResources().openRawResource(R.drawable.yangkai_photo);
OutputStream outputStream = new FileOutputStream(file);
//缓存机制
is = new BufferedInputStream(inputStream);
os = new BufferedOutputStream(outputStream);
//读写操作
int length = 0 ;
byte[] bytes = new byte[1024];
while ((length = is.read(bytes)) != -1){
os.write(bytes,0,length);
os.flush();//刷新操作
}
Toast.makeText(this,"图片已保存",Toast.LENGTH_SHORT).show();
ScanFile(file.getPath());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if ( is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//扫描进入图片
private void ScanFile(String path) {
MediaScannerConnection.scanFile(this,new String[]{path},null,null);
}
//显示图片
public void showImg(){
//从文件中读取出来
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
img.setImageBitmap(bitmap);
}
}