//参数声明 private final static int PHOTO_REQUEST_GALLERY = 1; private final static int PHOTO_CROP = 2;
//启动 Intent Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); startActivityForResult(intent, PHOTO_REQUEST_GALLERY);
// ActivityForResult的处理 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case PHOTO_REQUEST_GALLERY: if (data != null) { startPhotoZoom(data.getData()); } break; case PHOTO_CROP: if (data != null) { setPicToView(data); mSetV.setText("保存"); mCancelV.setVisibility(View.VISIBLE); is2Save = true; } break; default: break; } }
/** * 调用android系统自带的图片裁剪,把用户选择的图片 裁剪成适合用户手机的大小 * * @param imagePath */ public void startPhotoZoom(Uri uri){ // 调用intent跳转的裁间页面 Intent intent = new Intent("com.android.camera.action.CROP"); // 传入裁剪图片的路径uri和需要裁剪的格式 intent.setDataAndType(uri, "image/*"); // 可裁剪状态 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 72); intent.putExtra("outputY", 72); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTO_CROP); } private void setPicToView(Intent picdata) { Bundle bundle = picdata.getExtras(); if (bundle != null) { Bitmap photo = bundle.getParcelable("data"); photo.setDensity(Bitmap.DENSITY_NONE); mBitmap = photo; int w = photo.getWidth(); int h = photo.getHeight(); float f = photo.getDensity(); Drawable drawable = new BitmapDrawable(photo); headV.setImageDrawable(drawable); } }