我已经实现了图像捕获和裁剪功能的代码。 同样适用于联想A536和Huwaii Honor Holly,但不适用于Huwaii Ascend P7-L10。甚至所有这些都有Android 4.4.2与Loilipop更新。
第一个问题是,当我已经提供写入权限时,被捕获的图像没有存储在SD卡上。
其次是我无法以方形形式实现裁剪视图。只有上述手机从图库中选择图像时,最终用户才能调整。我的代码是:
public void selectGiniPicture(View view) {
isGiniImageSelected = true;
final CharSequence[] options = { "Take Picture", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(WorkerRegActivity.this);
builder.setTitle("Select Your Picture!");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Picture"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent();
//Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
//intent.putExtra("aspectX", 0);
//intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 225);
intent.putExtra("outputY", 225);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
执行相机的裁剪功能
private void performCrop() {
// take care of exceptions
try {
// call the standard crop action intent (the user device may not
// support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
//cropIntent.putExtra("aspectX", 0);
//cropIntent.putExtra("aspectY", 0);
// indicate output X and Y
cropIntent.putExtra("outputX", 225);
cropIntent.putExtra("outputY", 225);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 6);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
Toast toast = Toast
.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT);
toast.show();
}
}
OnActivityResult功能如下:
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
picUri = data.getData();
performCrop();
}
else if (requestCode == 2) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
capturedImage.setImageBitmap(photo);
}
}
}