//布局页面 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <Button android:id="@+id/btnOpenCamera" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="拍照立即显示" /> <Button android:id="@+id/btnSavePhoto" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="60dp" android:text="拍照存储后显示" /> <ImageView android:id="@+id/ivShowPicture" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_marginTop="130dp" /> </RelativeLayout> //主页面
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; import java.io.File; import java.io.FileInputStream; public class MainActivity extends Activity implements View.OnClickListener { private Button btnOpenCamera, btnSavePhoto; private ImageView ivShowPicture; private static int REQUEST_CAMERA_1 = 1; private static int REQUEST_CAMERA_2 = 2; private String mFilePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化控件 init(); // 控件绑定点击事件 bindClick(); } // 初始化控件和变量 private void init() { btnOpenCamera = (Button) findViewById(R.id.btnOpenCamera); btnSavePhoto = (Button) findViewById(R.id.btnSavePhoto); ivShowPicture = (ImageView) findViewById(R.id.ivShowPicture); mFilePath = Environment.getExternalStorageDirectory().getPath();// 获取SD卡路径 mFilePath = mFilePath + "/" + "temp.png";// 指定路径 } // 控件绑定点击事件 private void bindClick() { btnOpenCamera.setOnClickListener(this); btnSavePhoto.setOnClickListener(this); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.btnOpenCamera: // 拍照并显示图片 openCamera_1(); break; case R.id.btnSavePhoto: // 拍照后存储并显示图片 openCamera_2(); break; default: break; } } // 拍照并显示图片 private void openCamera_1() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机 startActivityForResult(intent, REQUEST_CAMERA_1); } // 拍照后存储并显示图片 private void openCamera_2() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 启动系统相机 Uri photoUri = Uri.fromFile(new File(mFilePath)); // 传递路径 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);// 更改系统默认存储路径 startActivityForResult(intent, REQUEST_CAMERA_2); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { // 如果返回数据 if (requestCode == REQUEST_CAMERA_1) { // 判断请求码是否为REQUEST_CAMERA,如果是代表是这个页面传过去的,需要进行获取 Bundle bundle = data.getExtras(); // 从data中取出传递回来缩略图的信息,图片质量差,适合传递小图片 Bitmap bitmap = (Bitmap) bundle.get("data"); // 将data中的信息流解析为Bitmap类型 ivShowPicture.setImageBitmap(bitmap);// 显示图片 } else if (requestCode == REQUEST_CAMERA_2) { FileInputStream fis = null; try { fis = new FileInputStream(mFilePath); // 根据路径获取数据 Bitmap bitmap = BitmapFactory.decodeStream(fis); ivShowPicture.setImageBitmap(bitmap);// 显示图片 } catch (Exception e) { e.printStackTrace(); } finally { try { fis.close();// 关闭流 } catch (Exception e) { e.printStackTrace(); } } } } } } //权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />