一、代码
-
xml布局中:两个按钮(分别从相机、相册获取照片)、一个显示图片的ImageView控件.
<Button android:id="@+id/bt_takePicture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="拍照" /> <Button android:id="@+id/bt_choosePicture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text="相册" /> <ImageView android:id="@+id/iv_picture" android:layout_width="wrap_content" android:layout_height="wrap_content" />
-
代码中
public class MainActivity extends Activity { public static final int TAKE_PICTURE = 1; // 拍照 public static final int CROP_PICTURE = 2; // 裁剪 private Uri imageUri; // 完整的file路径: ///图片地址 private ImageView iv_picture; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_picture = (ImageView) findViewById(R.id.iv_picture); findViewById(R.id.bt_takePicture).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 创建File对象:照片的保存路径 File outputImage = new File(Environment.getExternalStorageDirectory(), "temp.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PICTURE); // 启动裁剪 } }); findViewById(R.id.bt_choosePicture).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 创建File对象:照片的保存路径 File outputImage = new File(Environment.getExternalStorageDirectory(), "outputImage.jpg"); try { if (outputImage.exists()) { outputImage.delete(); } outputImage.createNewFile(); } catch (IOException e) { e.printStackTrace(); } imageUri = Uri.fromFile(outputImage); Intent intent = new Intent(Intent.ACTION_PICK); // 拿到本地已存在资源 intent.setType("image/*"); intent.putExtra("crop", true); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, TAKE_PICTURE); // 启动裁剪 } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case TAKE_PICTURE: if (resultCode == RESULT_OK) { if (data != null) { imageUri = data.getData(); } Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(imageUri, "image/*"); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); startActivityForResult(intent, CROP_PICTURE); // 启动裁剪程序 } break; case CROP_PICTURE: if (resultCode == RESULT_OK) { try { Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); /******************************** 图片可做压缩处理,防止OOM ********************************/ iv_picture.setImageBitmap(bitmap); // 将裁剪后的照片显示出来 } catch (FileNotFoundException e) { e.printStackTrace(); } } break; } } }
-
权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
二、分析
1.隐式启动系统相机 -> 返回照片至裁剪 -> 返回裁剪后的图片显示.
2.隐式启动系统相册 -> 返回照片至裁剪 -> 返回裁剪后的图片显示.
3.注意:显示图片前应该压缩,防止OOM.(本篇仅未做针对处理)
以上。如有错误和疑问,欢迎指正提出。 catface.wyh@gmail.com