Android 从相机裁剪照片
Posted
技术标签:
【中文标题】Android 从相机裁剪照片【英文标题】:Android crop photo from camera 【发布时间】:2014-07-14 10:03:01 【问题描述】:我想要从相机拍摄的裁剪照片,到目前为止我尝试这样做但没有成功
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
getActivity().startActivityForResult(intent, REQUEST_CAMERA);
是否可以在没有任何第 3 部分库的情况下做到这一点。? 我检查了https://github.com/biokys/cropimage
但它没有给我任何结果
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
String path = data.getStringExtra(CropImage.IMAGE_PATH);
// if nothing received
if (path == null)
return;
// cropped bitmap
Bitmap bitmap = BitmapFactory.decodeFile(path);
((ImageView) findViewById(R.id.userpicture)).setImageBitmap(bitmap);
【问题讨论】:
***.com/questions/10776438/… 【参考方案1】:是否可以在没有任何第 3 部分库的情况下做到这一点?
不可靠。 Android does not have a CROP
Intent
.
我检查了https://github.com/biokys/cropimage,但它没有给我任何结果
也许您没有正确集成它。除了我的博文中提到的库(链接到上面),the Android Arsenal has a few options。
【讨论】:
【参考方案2】:试试这个
private void performCrop(Uri picUri)
try
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", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe)
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
toast.show();
声明:
final int PIC_CROP = 1;
在顶部。
在onActivity结果方法中,编写如下代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP)
if (data != null)
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
imgView.setImageBitmap(selectedBitmap);
【讨论】:
以上是关于Android 从相机裁剪照片的主要内容,如果未能解决你的问题,请参考以下文章
android从图库中裁剪图像nullpointerexception