Android crop image in android 4.2.2 - android

I want to crop image after capturing from camera.
I am using this code.
Intent cropIntent = new Intent("com.android.camera.action.CROP");
but it gives error:
activity not found.
My android version is 4.2.2.
I also tried.
Intent intent = new Intent(Intent.ACTION_PICK);
but it opens the gallery not the captured image.

Use below code this will help you.
//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", 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);
for more details
http://mobile.tutsplus.com/tutorials/android/capture-and-crop-an-image-with-the-device-camera/

The below worked on my samsung galaxy s3. Check the snap shot
Use this for capture image with camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:src="#drawable/ic_launcher" />
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
private static final int PICK_FROM_CAMERA = 1;
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.button1);
buttonCamera.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// call android default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
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) {
}
}
});
}
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");
iv.setImageBitmap(photo);
}
}
}
}
Resulting snap shot

Related

Cant crop image from camera

First of all - im sorry for my bad english.
I try to get crop from camera photo.
This is my openCamera method:
private void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
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) {
// Do nothing for now
}
}
And here is ActivityForResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
photo = extras.getParcelable("data");
}
}
}
But its crashing with ...
03-28 14:40:41.791 15198-15198/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3050)
at dalvik.system.NativeStart.main(Native Method) 
Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.android.camera.action.CROP dat=file:///data/data/com.android.camera/files/crop-temp (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1518)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1390)
at
Absolutely no idea...
Use following code
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
and OnActivityResult() method
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");

Cropping not working in Nexus 5,Device Version 5.1.1

I want to get an image from gallery or camera and then I want to crop it in a particular aspect ratio.
But this code is working in Lollipop Version whereas it works in KitKat.
Can anyone plse helpme to solve this Problem.
public class MainActivity extends Activity {
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
ImageView imgview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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) {
// call android default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 2);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//// //TODO Auto-generated method stub
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 2);
intent.putExtra("aspectY", 2);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
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);
}
}
}
}
You must be getting extras2 as null in the last part right?
Same is happening with me! Could you explain what happens precisely? Does it crash the Photos app or you don't see the update in your imageView?

How to crop images from camera

How can I crop the camera images. Now it is showing the the image for crop and after selecting the crop section while tap on the "Save" button. Its showing as "saving image". After that nothing is happen. Here is my code.
Button click :
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);
intent.putExtra("return-data", true);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
onActivityResult :
Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
if (bitmap != null) {
Img_View.setImageBitmap(bitmap);
}
You can use this code to perform cropping:
.....
final int CAMERA_CAPTURE = 1;
final int CROP_PIC = 2;
private Uri picUri;
....
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button captureBtn = (Button) findViewById(R.id.capture_btn);
captureBtn.setOnClickListener(this);
}
public void onClick(View v) {
if (v.getId() == R.id.capture_btn) {
try {
// use standard intent to capture an image
Intent captureIntent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
// we will handle the returned data in onActivityResult
startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
Toast toast = Toast.makeText(this, "This device doesn't support the crop action!",
Toast.LENGTH_SHORT);
toast.show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_CAPTURE) {
// get the Uri for the captured image
picUri = data.getData();
performCrop();
}
// user is returning from cropping the image
else if (requestCode == CROP_PIC) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
ImageView picView = (ImageView) findViewById(R.id.picture);
picView.setImageBitmap(thePic);
}
}
}
/**
* this function does the crop operation.
*/
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", 2);
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, CROP_PIC);
}
// 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();
}
}
You can use following simple tutorial to perform cropping:
http://khurramitdeveloper.blogspot.in/2013/07/capture-or-select-from-gallery-and-crop.html
http://www.londatiga.net/featured-articles/how-to-select-and-crop-image-on-android/
http://www.coderzheaven.com/2012/12/15/crop-image-android/
http://shaikhhamadali.blogspot.in/2013/09/capture-images-and-crop-images-using.html
Save yourself a lot of time and use this library I was messing around with trying to do it myself and stumbled on this library and its really simple to use and you get a professional looking image cropping view that lets you choose camera or photo library.
Simple example:
Include the library in your gradle
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
Add permissions to manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
//add this under <application>
<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="#style/Base.Theme.AppCompat"/>
In your activity
//on button press or anywhere, this starts the image picking and cropping process
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);
//in your activity where you will get the result of your cropped image
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
//From here you can load the image however you need to, I recommend using the Glide library
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}
Im not affiliated with this software
Try this
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), "tmp_avatar_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);

How to select a piece of an image to set the avatar

I've an application where user can set his own avatar by taking himself a picture or just picking a picture from the gallery. I've seen in other apps that user, after pick an image its displayed a view where user can "draw" a rectangle selecting which area of the image he wants to use as avatar.
I would like to include this possibility in my app. How can I do that after taking the picture?
Thanks!
Edit:
I'm trying to do it with this but it opens image gallery instead of camera:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.setType("image/*");
cameraIntent.putExtra("crop", "true");
cameraIntent.putExtra("scale", "true");
cameraIntent.putExtra("outputX", 100);
cameraIntent.putExtra("outputY", 100);
cameraIntent.putExtra("aspectX", 1);
cameraIntent.putExtra("aspectY", 1);
cameraIntent.putExtra("max-width", 30);
cameraIntent.putExtra("max-height", 30);
cameraIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(cameraIntent, IMAGEN_CAMARA);
This is how i implemented it in one of my apps that uses this feature. It is quite simple.
private void doTakePhotoAction() {
// http://2009.hfoss.org/Tutorial:Camera_and_Gallery_Demo
// http://stackoverflow.com/questions/1050297/how-to-get-the-url-of-the-captured-image
// http://www.damonkohler.com/2009/02/android-recipes.html
// http://www.firstclown.us/tag/android/
// The one I used to get everything working: http://groups.google.com/group/android-developers/msg/2ab62c12ee99ba30
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Wysie_Soh: Create path for temp file
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"tmp_contact_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
//Do nothing for now
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) {
return;
}
switch (requestCode) {
case CROP_FROM_CAMERA: {
//Wysie_Soh: After a picture is taken, it will go to PICK_FROM_CAMERA, which will then come here
//after the image is cropped.
final Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
mPhoto = photo;
mPhotoChanged = true;
mPhotoImageView.setImageBitmap(photo);
setPhotoPresent(true);
}
//Wysie_Soh: Delete the temporary file
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) {
f.delete();
}
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mPhotoImageView, InputMethodManager.SHOW_IMPLICIT);
break;
}
case PICK_FROM_CAMERA: {
//After an image is taken and saved to the location of mImageCaptureUri, come here
//and load the crop editor, with the necessary parameters (96x96, 1:1 ratio)
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 96);
intent.putExtra("outputY", 96);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_FROM_CAMERA);
break;
}
}
}
Hope it helps :)

Issue in Picking image From gallery

I am using following code to pick image from gallery
public void takePhotoFromLibrary() {
_isFromLogin = false;
try {
// Launch picker to choose photo for selected contact
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
// intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG
.toString());
intent.putExtra("noFaceDetection", false);
startActivityForResult(intent, PHOTO_PICKED);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
}
I am using Lg optimus p350 for testing. In this case when i select an image picked by camera onActivityForResult is not getting called. Can anyone please help me with this?
It may be help you..
http://android-er.blogspot.com/2011/02/select-image-using-android-build-in.html
How to pick an image from gallery (SD Card) for my app?
If you use this intent instead to launch the pick image activity:
Intent i = new Intent(Intent.ACTION_PICK, Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, PICK_IMAGE_REQUEST_CODE);
Then you will get a callback to:
#Override
protected void onActivityResult(int requestCode, int resultCode, final Intent intent) { }
when the user has selected an image.

Categories

Resources