Android 调用系统相机和相册
MainActivity
package com.example.photoxc_demo; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.Bundle; import android.provider.MediaStore; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity implements View.OnClickListener { private Button btnCamera; private Button btnGallery; private ImageView imageView; private Intent intent; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnCamera = (Button) findViewById(R.id.mBtn1); btnCamera.setOnClickListener(this); btnGallery = (Button) findViewById(R.id.mBtn2); btnGallery.setOnClickListener(this); imageView = (ImageView) findViewById(R.id.mImg); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Bitmap bitmap=CameraGallaryUtil.getBitmapFromCG(this,requestCode,resultCode,data); imageView.setImageBitmap(bitmap); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.mBtn1: // 利用系统自带的相机应用:拍照 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 此处这句intent的值设置关系到后面的onActivityResult中会进入那个分支,即关系到data是否为null // 如果此处指定,则后来的data为null // 只有指定路径才能获取原图 intent.putExtra(MediaStore.EXTRA_OUTPUT, CameraGallaryUtil.fileUri); startActivityForResult(intent, CameraGallaryUtil.PHOTO_REQUEST_TAKEPHOTO); break; case R.id.mBtn2: intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, CameraGallaryUtil.PHOTO_REQUEST_GALLERY); break; } } }
CameraGallaryUtil
package com.example.photoxc_demo; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; public class CameraGallaryUtil { private static final int RESULT_OK = -1; public static final int PHOTO_REQUEST_TAKEPHOTO = 1;// 拍照 public static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 public static final int PHOTO_REQUEST_CUT = 3;// 裁剪结果 public static final Uri fileUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory() .getPath() + File.separator + "temp.jpg"));//临时储存的Uri public static Bitmap getBitmapFromCG(Activity activity, int requestCode, int resultCode, Intent data) { Bitmap bitmap = null; switch (requestCode) { // 如果是拍照 case PHOTO_REQUEST_TAKEPHOTO: if (resultCode == RESULT_OK) { // 没有指定特定存储路径的时候,data不为null if (data != null) { if (data.getData() != null) { startPhotoZoom(activity, data.getData()); } } else { startPhotoZoom(activity, fileUri); } } break; // 如果是从相册选取 case PHOTO_REQUEST_GALLERY: if (data != null) { if (data.getData() != null) { startPhotoZoom(activity, data.getData()); } } break; //如果是裁剪完成 case PHOTO_REQUEST_CUT: if (data != null) { Bundle bundle = data.getExtras(); if (bundle != null) { bitmap = bundle.getParcelable("data"); } } break; } return bitmap; } private static void startPhotoZoom(Activity activity, Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // crop为true是设置在开启的intent中设置显示的view可以剪裁 intent.putExtra("crop", "true"); //aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 300); intent.putExtra("aspectY", 400); //outputX,outputY 是剪裁图片的宽高 intent.putExtra("outputX", 300); intent.putExtra("outputY", 400); intent.putExtra("return-data", true); activity.startActivityForResult(intent, PHOTO_REQUEST_CUT); } }
//以下是xml布局:
activity
<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="com.example.photoxc_demo.MainActivity" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/mBtn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="相机" /> <Button android:id="@+id/mBtn2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="相册" /> <ImageView android:id="@+id/mImg" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>
//需要添加的权限:
<uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.per
