Intent cropIntent = new Intent("com.android.camera.action.CROP", null);
Package: "com.android.camera.action" is not available in all the android devices(like Nexus 7).Is there any code to get the name of "crop package" used by that android device.
have you check this
Android 4.3 crop gallery resultCode Cancel
Have you considered just using a library like this one:
https://github.com/biokys/cropimage
I find the com.android.camera.action.CROP can sometimes behave differently from phone to phone and is not always available, so it could cause some problems for you anyway if you are looking to release it.
UPDATE:
I have tested the above library with Android 4.3 and it works with no problem. You just need to add the library to your project.
You can then write your method in a very similar way:
private void performCrop(Uri picUri) {
//you have to convert picUri to string and remove the "file://" to work as a path for this library
String path = picUri.toString().replaceAll("file://", "");
try {
int aspectX = 750;
int aspectY = 1011;
Intent intent = new Intent(this, CropImage.class);
//send the path to CropImage intent to get the photo you have just taken or selected from gallery
intent.putExtra(CropImage.IMAGE_PATH, path);
intent.putExtra(CropImage.SCALE, true);
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath)));
startActivityForResult(intent, CROP);
}
catch (ActivityNotFoundException anfe) {
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Related
My app is using a custom type of file that can be saved onto the external memory and shared.
I want to use the file picker to pick one file and load it. Here is my code :
Uri uri = Uri.fromFile("path/to/folder");
Intent intent = new Intent(Intent.ACTION_PICK, uri);
try {
startActivityForResult(Intent.createChooser(intent, "Select a file"), 123);
} catch (android.content.ActivityNotFoundException e) {
Toast.makeText(this, "Error fileChooser",Toast.LENGTH_SHORT).show();
Log.e("Dan", "onOptionsItemSelected: Error filechooser ActivityNotFoundException", e);
}
This code works absolutely perfectly on my genymotion emulator and my Nexus 4 on CyanogenMod and call the file manager to browse to the folder.
But when I try on my brand new LG G4, it doesn't work.
The biggest problem is that I get no error message, just a file picker with the good title and a message "No app can perform this action".
How to check if a file picker is available? (to provide a simple alternative if needed)
Try this:
PackageManager packageManager = getActivity().getPackageManager();
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
PackageManager.GET_ACTIVITIES));
Then, check the size of the list, if it is 0, then there is no file explorer.
I tried below code to crop large image with pixel (2448*3264). But, This process is not working fine. Please correct the below code or give me another solution to achieve this images cropping process. Advance thanks to all.
private void performCrop(){
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(UrlGambar, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 3);
//cropIntent.putExtra("outputX", 256);
//cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
}
catch(ActivityNotFoundException anfe){
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
Intent com.android.camera.action.CROP is not a standard android intent and there are many devices that donot support this intent. Please refer to my answer in this post. I am using a library from github to do the cropping.
My Galaxy Nexus is now running on Android 4.3 allowing me to test my application with this new version. Everything seems fine except cropping.
I have an application that uses the camera to take picture and then crop the image through the gallery app.
I am also able to choose a picture from the gallery and crop it after.
Since Android 4.3, the gallery application changed.
If i take a picture with the camera api and then ask the gallery to crop it in my onActivityResult method the resultCode is set to 0 (meaning cancel) whereas i clicked on "Save" from the crop view.
But if i choose a picture from the gallery and crop it everything works, the resultCode parameter is set to -1.
I call the same method to crop the picture in both cases.
I have quickpic (an alternative to the gallery app) on my phone, with it everything works !
private void performCrop(Uri picUri) {
try {
int aspectX = 750;
int aspectY = 1011;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(picUri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("scale", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath)));
startActivityForResult(intent, CROP);
}
catch (ActivityNotFoundException anfe) {
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Everything worked fine on Android 4.2.2.
Thank you for your help !
Have you considered just using a library like this one:
https://github.com/biokys/cropimage
I find the com.android.camera.action.CROP can sometimes behave differently from phone to phone and is not always available, so it could cause some problems for you anyway if you are looking to release it.
UPDATE:
I have tested the above library with Android 4.3 and it works with no problem. You just need to add the library to your project.
You can then write your method in a very similar way:
private void performCrop(Uri picUri) {
//you have to convert picUri to string and remove the "file://" to work as a path for this library
String path = picUri.toString().replaceAll("file://", "");
try {
int aspectX = 750;
int aspectY = 1011;
Intent intent = new Intent(this, CropImage.class);
//send the path to CropImage intent to get the photo you have just taken or selected from gallery
intent.putExtra(CropImage.IMAGE_PATH, path);
intent.putExtra(CropImage.SCALE, true);
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentPhotoPath)));
startActivityForResult(intent, CROP);
}
catch (ActivityNotFoundException anfe) {
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Above library is only useful if your cropping into a lot smaller images. If you want to crop to a better resolution images, it is best to use the Android Crop Intent.
picUri must be a valid URI which points to your image and outputUri should be a new file you created for writing the cropped image. It works on all devices and 4.3 Source code does indeed have com.android.camera.action.CROP intent available for usage. I've tested this on many devices and it works well.
private void performCrop(Uri picUri, Uri outputUri) {
try {
int aspectX = 2000;
int aspectY = 1200;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(picUri, "image/*");
intent.putExtra("scale", "true");
intent.putExtra("aspectX", aspectX);
intent.putExtra("aspectY", aspectY);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", false);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
startActivityForResult(intent, CROP);
}
catch (ActivityNotFoundException anfe) {
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
I've encountered this problem on a Nexus 10 as well. the crop intent returns a cancelled code. after some tweaking I've found a solution:
In my case the input file set in setDataAndType() was the same file as the output set using the MediaStore.EXTRA_OUTPUT extra. Using the same file for input and output worked fine on most devices, specifically on devices below 4.3. However on 4.3 it would result in canceled crops. Simply using different files for input and output resolved the issue.
So what you need to make sure of is that your picUri parameter points to a file that is not the same as your mCurrentPhotoPath. I'm not sure what exactly changed from 4.2 to 4.3 to cause this issue. But using different files seems to resolve it easily.
I am creating an android application. It takes a picture then allows you to crop it and then displays it. Problem is that it only saves the taken image not the cropped one. Basically i need it to save the cropped image. how can I save a file after cropping?
Code:
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", 1);
cropIntent.putExtra("aspectY", 1.5);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return", 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 = "Your device does not support cropping";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Just add something like this:
try{
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
), fileName+".png"); //save to your pictures folder
outputFileURI = Uri.fromFile(file);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileURI);
startActivityForResult(cropIntent, PIC_CROP);
} catch (IOException ioe){
// handle your exception
}
Remember to refresh the gallery after saving so that its instantly available in the gallery. So use this code maybe in your onActivityResult method?
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse
("file://" + Environment.getExternalStorageDirectory())));
Edit: Found a better way to refresh the gallery since sendBroadcast can be a inefficient if you are just refreshing for one image. Use a MediaScanner to scan the file like so
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(outputFileURI); // Add the path to the file
sendBroadcast(intent);
This will just scan for the new file and refresh that instead of the whole gallery.
so I want to choose an image from gallery and then crop it:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), PHOTO_PICKED_WITH_DATA);
OK, pick the photo and then catch it onActivityResult, then crop it:
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mAvatarUri, "image/*");
intent.putExtra("crop", true);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", ICON_SIZE);
intent.putExtra("outputY", ICON_SIZE);
intent.putExtra("scale", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mAvatarUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_IMAGE);
now, the problem is when i want to transform it into bytes and then send it inside an xml... it doesn't take only the cropped image but instead the whole image itself...
also, i cannot access cropped image uri, it says file not found!
hmmmm, seems like my cropped image weren't saved after all...
how can i suppose to fix this?
Samsung ACE 2.3.4
Check this code in the following link.
Crop Image
It works fine for me..
I don't know how you got that technique to crop image. But, for me, I always use this library. And it never failed to impress me. Work from android 2.1 all the way to 3.2 (never test it on 4.0 onward).
Here's how I do it:
Intent cropIntent = new Intent(imageProcessActivity,
CropImage.class);
cropIntent.putExtra("image-path",
FileUtil.saveTempFile(ImageProcessActivity.processedBitmap, filename));
cropIntent.putExtra("scale", true);
imageProcessActivity.startActivityForResult(cropIntent, ImageProcessActivity.INTENT_CROP_CODE);
and here's how to catch the result:
if (requestCode == INTENT_CROP_CODE && resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
if (extras != null) {
Uri uri = null;
uri = (Uri) extras.get("imageCrop");
Bitmap bitmap = null;
try {
bitmap = ImageUtil.decodeFile(
new File(new URI(uri.toString())),
AppConstant.MAX_IMAGE_SIZE);
} catch (URISyntaxException e) {
e.printStackTrace();
}
processedBitmap = bitmap;
selectedImage.setImageBitmap(bitmap);
}
}