Crop Intent Not Working in Android Studio - java

I have a problem regarding cropping image in android studio, whenever I run this cropping code on my device or any other One-plus device, it runs efficiently. But other devices like Redmi, Samsung, Motorola, crash after reaching this cropping part. if I comment out the function call to the cropping function, it runs smoothly on all devices but at cost of non-availability of cropping
public void ImageCropFunction(Uri uri) {
// Image Crop Code
try {
Intent CropIntent = new Intent("com.android.camera.action.CROP");
Toast.makeText(getContext(),"plz, Crop the Required part",Toast.LENGTH_LONG).show();
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 1024);
CropIntent.putExtra("outputY", 1024);
CropIntent.putExtra("return-data", true);
CropIntent.putExtra("return-uri", uri.toString());
startActivityForResult(CropIntent, 222);
}catch (ActivityNotFoundException e) {
}
}

What to use
You can use uCrop library.
Implemention
Make sure you have this line in your settings.gradle
maven { url "https://jitpack.io" }
Add it to your build.gradle.
implementation 'com.github.yalantis:ucrop:2.2.6'
Then you should add it to your manifest
<activity
android:name="com.yalantis.ucrop.UCropActivity"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"/>
How to use
You can crop a image like this
UCrop.of(yourImageUri, whereToSaveYourCroppedImageUri)
.withAspectRatio(16, 9) // you can change the aspect ratio.
.withMaxResultSize(maxWidth, maxHeight) // you can add a custom result height for the image. eg 512 X 512
.start(context); // enter the context and the crop will start.
Get the result
You can fetch the result in the onActivityResult like this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == UCrop.REQUEST_CROP) {
final Uri resultUri = UCrop.getOutput(data);
// crop is successful
} else if (resultCode == UCrop.RESULT_ERROR) {
final Throwable cropError = UCrop.getError(data);
// crop failed
}
}
Output
View the output gif from here

Android does not have a CROP Intent. There is no requirement for any device to support that undocumented Intent, let alone with those undocumented extras. There are dozens of libraries for image cropping. Please use one.

Related

Show an image in opengl is working with pictures from gallery but from camera doesn't

I have some troubles to display images in OpenGL.
Actually I'm able to display images from gallery in opengl. The problem occurs when I try to show one from the camera.
For me, OpenGL have to display the image from the camera as it does with the gallery ones. Obviously I'm making something wrong.
Any help will be appreciated.
Intent from gallery:
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult(intent, 2);
Intent from camera:
Intent takePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePic.resolveActivity(getPackageManager()) != null) {
File imagen = controler.createPhotoFile(getExternalFilesDir(Environment.DIRECTORY_PICTURES));
if (imagen != null) {
photoUri = FileProvider.getUriForFile(this, "my.fileprovider", imagen);
takePic.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(takePic, 1);
}
}
This is my onActivityResult where I send the URI to a method which convert it to a bitmap and send it.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 1:
sendImagenPanel(photoUri);
break;
case 2:
sendImagenPanel(data.getData());
break;
}
}
}
private void sendImagenPanel(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
final Bitmap imagen = controler.getCroppedBitmap(controler.scaledBitmap(bitmap, 256));
final CasillaOG casilla = ((GLSurfacePanel) gLViewPanel).getRendererPanel().getCuboSelected();
gLViewPanel.queueEvent(new Runnable() {
#Override
public void run() {
casilla.loadNewTexture(imagen);
casilla.setImagen(imagen);
}
});
gLViewPanel.requestRender();
}
In case someone is interested. I realize that the problem is not on the method that calls OpenGL. If I run the same code on the onActivityResult works from the gallery requestCode but not on the camera one, in my Samsung Galaxy Tab A. Why I mention my device? because if I run the app on a Huawei P9 lite, the gallery images are not display either. In both cases appears the next problem on the console:
call to opengl es api with no current context (logged once per thread)
After search that problem, I suppose that the intents of the camera and gallery use OpenGL and its originate a conflict with my own OpenGL environment.
Finally, I opted to set a bitmap field and add the texture in on the onDrawFrame. Obviously, with a boolean to make it one time.

Android get URI of cropped image from Intent

I'm trying to capture an image from the Android Camera/ Pick an image from the gallery and then crop it before performing other operations on it. I'm having trouble with getting back the URI of the cropped image. Any help on how to get back the URI of the image once it has been cropped would be much appreciated!
The code below pertains to my onActivityResult and my function performing the crop
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE || requestCode == REQUEST_IMAGE_CAPTURE) {
if(!IS_CAMERA_USED) {
selectedImageUri = data.getData();
}
performCrop();
}
else if(requestCode == CROP_IMAGE){
selectedImageUri = data.getData();
onSelectedImageResult();
}
}
}
public void performCrop() {
// take care of exceptions
Display display = getWindowManager().getDefaultDisplay();
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(selectedImageUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectY", 1);
if(IS_PROFILE_PICTURE) {
cropIntent.putExtra("aspectX", 1);
}
else {
cropIntent.putExtra("aspectX", 2);
}
// indicate output X and Y
cropIntent.putExtra("outputX", display.getWidth());
cropIntent.putExtra("outputY", display.getHeight());
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
}
// 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();
}
}
The problem is in the line selectedImageUri = data.getData(), where data.getData() returns null after having done the crop. How do I get back the URI of the cropped image? I don't want to get data.getExtras.getParcelable("data") as that returns the thumbnail and ruins the image resolution.
Thanks in advance!
You can get the image bitmap with this code:
if (requestCode == CROP_IMAGE) {
if (data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap selectedBitmap = extras.getParcelable("data");
//imgView.setImageBitmap(selectedBitmap);
}
}
}
URI is not possible I think, because the cropped image is not saved on the storage.
Thanks for all your answers, but I found something way simpler to incorporate and use that avoids all hassles due to the `com.android.camera.action.CROP' class, and it can be found here
I think that you should save the cropped Bitmap to your storage, and then use the URI of the cropped Bitmap to performe something over it!
I know it's not a professional work but at least it should do it

How to skip or avoid 'retake and review' option after capturing photo from camera using ACTION_IMAGE_CAPTURE

I want to display the image when I click on the photo and want to set in my ImageView without user select yes or not....
I had searched for it and I also know it very well that the camera app itself gives you the ability to review/retake the image, and once an image is accepted, the activity displays it. But, I want to do it without review/retake the activity display it.....
I am trying this code fine
Initialise
Uri mImageCaptureUri;
For Click on Button
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
File file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(MediaStore.AUTHORITY, true);
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = null;
mPath = mImageCaptureUri.getPath();
System.out.println("THE PAtH:_" + mPath);
BitmapFactory.Options o2 = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(mPath, o2);
ivSelfie.setImageBitmap(bitmap);
}
When I am Click the Photo Than I am Take this screen to select yes or not......
But My requirement is not select review/retake task and direct set to ImageView on activity display when just click and set.....
Actually it's quite useful to have confirmation of taken picture. But, in case if you really don't want to have it, you have to use SurfaceView inside your app and show camera stream here. There is tones of example how to do it, for example consider to check that one.
Use method setImageURI() it will get the bitmap from the uri and set it for you.
Yes it will set the image weather the user press ok or cancel no matter because your file exists on your given path while launching intent.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_CAMERA) {
// only one line code
ivSelfie.setImageURI(mImageCaptureUri);
}
}
You cannot avoid that screen. The reason is that when you use new MediaStore.ACTION_IMAGE_CAPTURE you are using a different camera application for clicking image. Showing this screen might be the default functionality of the. This screen will be different in different devices depending upon the camera application.
So to get rid of this the only thing you can do is to implement your custom camera instead of using default camera application.
As per default camera app you can't breach that functionality. Instead of, you can use SurfaceView to capture image inside you application. Please have a look at this answer thread.
May be this link will help you.
Thank you
Use "android.intent.extra.quickCapture" for your picture intent.
// File ImagePickerModule.java#248
cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("android.intent.extra.quickCapture",true);
See https://github.com/react-native-community/react-native-image-picker/issues/647 for details.

Android image from camera

My app is using image from phone camera or from image gallery. The issue is when picture from gallery/library is taken with front facing camera, because it would have diferent orientation than back camera. Is there any way to get info on which camera was used for thaking the image (was if front or back)? I can get orientatin of image, but no origin of image (aka front or back camera).
For example, this is how I open native camera app:
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Get file location.
imagePath ip = new imagePath();
File file = ip.getFile();
// Put extra arguments into activity.
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
// Start activity.
startActivityForResult(camera_intent, CAM_REQUEST);
And in onActivityResult I have this code:
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
switch(requestCode) {
case CAM_REQUEST:
// Check result.
if (resultCode != 0){
readBitmapInfo(); // Here I get image from stored location.
}
}
}
Any sugestion?

How do I crop a drawable in Android?

I am developing an Android application that allows a user to crop an image which is in my Drawable folder. This is the code I am using but it is showing errors. How can I fix this?
#Override
public void onClick(View v) {
Uri imgUri=Uri.parse("android.resource://com.example.cropapp/"+R.drawable.apples);
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(imgUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
startActivityForResult(intent, 1);
}
How can I fix this?
First, recognize that Android does not have a CROP Intent. There are many image cropping libraries for Android. Use one.
Second, recognize that few, if any, apps advertise <intent-filter> structures that support the ill-used android.resource scheme. This will not be a problem when you switch to an image-cropping library, as then everything will be in your own app.
However, do bear in mind that the vast majority of image-cropping scenarios involve images that are files, not resources, let alone drawable resources. It is entirely possible that you are the first person in human history to want to allow users to crop drawable resources, and so you are likely to have to blaze your own trail a bit.
The following code snippet is for custom roms like AOSP Camera app, and not Officially available or supported by most of the devices
Possible Solution?
Try an unofficial library like this one,
https://github.com/lvillani/android-cropimage
Code example:
#Override
public void onClick(View view) {
if (view.equals(button)) {
startActivityForResult(MediaStoreUtils.getPickImageIntent(this), REQUEST_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
File croppedImageFile = new File(getFilesDir(), "test.jpg");
if ((requestCode == REQUEST_PICTURE) && (resultCode == RESULT_OK)) {
// When the user is done picking a picture, let's start the CropImage Activity,
// setting the output image file and size to 200x200 pixels square.
Uri croppedImage = Uri.fromFile(croppedImageFile);
CropImageIntentBuilder cropImage = new CropImageIntentBuilder(200, 200, croppedImage);
cropImage.setOutlineColor(0xFF03A9F4);
cropImage.setSourceImage(data.getData());
startActivityForResult(cropImage.getIntent(this), REQUEST_CROP_PICTURE);
} else if ((requestCode == REQUEST_CROP_PICTURE) && (resultCode == RESULT_OK)) {
// When we are done cropping, display it in the ImageView.
imageView.setImageBitmap(BitmapFactory.decodeFile(croppedImageFile.getAbsolutePath()));
}
}

Categories