我试图让用户在拍摄/从图库中选择后裁剪图像。现在,从画廊作品而不是相机中选择后的裁剪。以下是我的代码,我没有收到任何错误消息。我们关注了 http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/
//Camera button clicked
public void camera_click(View view) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
//Result of camera capture
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//camera
if (requestCode == CAMERA_PIC_REQUEST) {
mImageCaptureUri = data.getData();
performCrop();
}
if (requestCode == PIC_CROP) {
//get the returned data
try{
Bundle extras = data.getExtras();
//get the cropped bitmap
thumbnail = extras.getParcelable("data");
//retrieve a reference to the ImageView
ImageView image = (ImageView) findViewById(R.id.test_image);
//display the returned cropped image
image.setImageBitmap(thumbnail);
TextView imgTv = (TextView) findViewById(R.id.imageInfo);
String desc = imgTv.getText().toString();
if (desc.equalsIgnoreCase("")) {
String errorMessage = "Please enter a description before submitting a photo.";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
} else {
}
}
catch(Exception ex)
{
Log.i("err", ex.getMessage());
}
}
//gallery selected
if (requestCode == SELECT_PHOTO) {
if (data != null) {
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst(); //if not doing this, 01-22 19:17:04.564: ERROR/AndroidRuntime(26264): Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
int idx = cursor.getColumnIndex(ImageColumns.DATA);
String fileSrc = cursor.getString(idx);
thumbnail = BitmapFactory.decodeFile(fileSrc); //load preview image
thumbnail = android.graphics.Bitmap.createScaledBitmap(thumbnail, 100, 100, true);
// ImageView image = (ImageView) findViewById(R.id.test_image);
// image.setImageBitmap(thumbnail);
ImageButton cam = (ImageButton) findViewById(R.id.camera);
// cam.setVisibility(View.INVISIBLE);
ImageButton gal = (ImageButton) findViewById(R.id.gallery);
mImageCaptureUri = data.getData();
// gal.setVisibility(View.INVISIBLE);
performCrop();
} else {
//Log.d(LOG_TAG, "idButSelPic Photopicker canceled");
// m_Tv.setText("Image selection canceled!");
}
}
}
}//end onactivity results
//method to luanch crop image
private void performCrop() {
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
//display an error message
Log.i("err", anfe.getLocalizedMessage());
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
Intent myIntent = new Intent();
startActivityForResult(myIntent, PIC_CROP);
}
}