来自第一行代码笔记
1、调用摄像头活动拍照
2、拍照完返回到onActivityResult,裁剪
3、裁剪完返回到onActivityResult,显示图片
4、从相册选取图片,启动裁剪,显示图片
package com.example.choosepictest; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; 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.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { public static final int TAKE_PHOTO = 1; public static final int CROP_PHOTO = 2; private Button takePhoto; private Button chooseFromAlbum; private ImageView picture; private Uri imageUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); takePhoto = (Button) findViewById(R.id.take_photo); chooseFromAlbum = (Button) findViewById(R.id.choose_from_album); picture = (ImageView) findViewById(R.id.picture); takePhoto.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //把图片命名为output_image.jpg并保存在sd卡根目录 File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } //Uri对应outputImage唯一地址 imageUri = Uri.fromFile(outputImage); //调用拍照活动 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); //指定图片输出地址 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //启动拍照活动 startActivityForResult(intent, TAKE_PHOTO); } }); chooseFromAlbum.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { File outputImage = new File(Environment.getExternalStorageDirectory(), "output_image.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); intent.putExtra("crop", true); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PHOTO);//启动裁剪活动 } }); } //结果返回,拍照和裁剪完返回 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { //拍照完 case TAKE_PHOTO: if (resultCode == RESULT_OK) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*");//设定图片类型 intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//键:用于后面从Intent取值;要传递的数据 startActivityForResult(intent, CROP_PHOTO);//启动裁剪程序 } break; //裁剪完 case CROP_PHOTO: if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver() .openInputStream(imageUri)); //显示image picture.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; default: break; } } }
结果: