[英]How to pick image from camera and gallery , crop it and set to image view
[英]How to crop an image while taking it from the gallery and display it in an Image view
当我单击按钮时,将带我到图库中选择图像,但是选择图像后,它将立即回到活动状态,而不会进行裁剪。 此外,图像不会显示在imageView中。
这是我的代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
imgview = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
buttonCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
}
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
imgview.setImageBitmap(photo);
}
}
if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable("data");
imgview.setImageBitmap(photo);
}
}
}
}
但它会返回到活动,而不会裁剪
这是因为不需要在任何地方进行任何活动来兑现那些随机的Intent
附加功能,更不用说用户为您的特定请求选择的任何活动了(基于他们的设备以及已安装的ACTION_IMAGE_CAPTURE
和ACTION_GET_CONTENT
活动)。 您会注意到,这些额外功能均未记录在ACTION_IMAGE_CAPTURE
或ACTION_GET_CONTENT
。
有许多可用于Android的图像裁剪库 。 使用一个。
此外,图像不会显示在imageView中。
这是因为ACTION_GET_CONTENT
不会通过data
Intent
Extra返回任何内容。 使用ACTION_GET_CONTENT
返回的Uri
(通过传递给onActivityResult()
的Intent
上的getData()
获得)。 将Uri
提供给您喜欢的图像加载库(Picasso,Universal Image Loader等)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.