Editing is not supported for this Image Cropping an Image - android

I have a picture I want to post as imageButton but before posting it I have a cropper:
private void CropImage() {
try {
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(uri, "image/*");
CropIntent.putExtra("crop", "true");
CropIntent.putExtra("outputX", 180);
CropIntent.putExtra("outputY", 180);
CropIntent.putExtra("aspectX", 3);
CropIntent.putExtra("aspectY", 4);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent , 1);
}
catch (ActivityNotFoundException ex){
}
}
And when I select a picture from gallery or take a picture with my camera it shows a toast Editing is not supported for this Image, and I'm not sure what's really causing this.
This is my GalleryOpen() and CameraOpen():
private void GalleryOpen() {
GalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(GalleryIntent, "Select Images From Gallery"), 2);
}
private void CameraOpen() {
CamIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
"file"+String.valueOf(System.currentTimeMillis())+ ".jpg");
uri = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID + ".provider", file);
CamIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
CamIntent.putExtra("return-data", true);
CamIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(CamIntent, 0);
}
And my OnActivityResult:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && resultCode == RESULT_OK)
CropImage();
else if(requestCode == 2) {
if (data != null) {
uri = data.getData();
CropImage();
}
}
else if(requestCode == 1) {
if(data != null){
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
imageHolder.setImageBitmap(bitmap);
}
}
}

Related

Camera Crop-Code does not work on Android 7.0

My Code works on all devices Android 4 till Android 6.x
But after I have updated my device on Android 7.0, the camera code does not work more. I get black screen !
I can make capture from camera but after that i get back screen if I want to crop the image
may be the crop funktion does not get the corect path of the bitmap
screen
any Idea? here is the code :
img = (ImageView) findViewById(R.id.imageView);
img_original = (ImageView) findViewById(R.id.imageView_original);
}
public void Capture(View view) {
Capture_Cam();
}
private void Capture_Cam() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK && data != null) {
picUri = data.getData();
try {
// bitmap2 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), picUri); // bitmap2 = original before cur
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse("file://"+picUri));
img_original.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
performCrop();
} else if (requestCode == 2) {
bitmap2=(Bitmap) data.getExtras().get("data");
img.setImageBitmap(bitmap2);
}else{
super.onActivityResult(requestCode, resultCode, data);
}
}
private void performCrop(){
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 100);
cropIntent.putExtra("outputY", 100);
cropIntent.putExtra("scale", true);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, 2);
Toast toast = Toast.makeText(this, "Done", Toast.LENGTH_SHORT);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}

pick image from gallery and crop it with sizes larger than 500*500

I want to select image from gallery and crop it with 800*600 size, but with sizes larger than 500*500 it is not working!! how can I do it?
my code is as below:
public void showFileChooser() {
Intent imageDownload = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imageDownload.putExtra("crop", "true");
imageDownload.putExtra("aspectX", 4);
imageDownload.putExtra("aspectY", 3);
imageDownload.putExtra("outputX", 800);
imageDownload.putExtra("outputY", 600);
imageDownload.putExtra("return-data", true);
startActivityForResult(imageDownload, 2);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK && null != data) {
Bundle extras = data.getExtras();
bitmap1 = extras.getParcelable("data");
imageView1.setImageBitmap(bitmap1);
}
}
Try this, it works for me, Hope it'll help you too....
1 - Choose image from gallery
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Select File"),Util.REQUEST_GALLERY);
2 - Crop image in onActivityResult as below
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == getActivity().RESULT_OK) {
switch (requestCode) {
case Util.REQUEST_GALLERY:
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) && !Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED_READ_ONLY)) {
File file = new File(Uri.parse(Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator+ ".MyImages"+ File.separator+ "picture").getPath());
if (!file.exists()) {
file.mkdirs();
}
selectedPath1 = File.createTempFile("myImages"+ new SimpleDateFormat("ddMMyyHHmmss",Locale.US).format(new Date()),".jpg", file).toString();
croppedImageUri = Uri.fromFile(new File(selectedPath1));
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(data.getData(), "image/*");
intent.putExtra("outputX", 700); // pass width
intent.putExtra("outputY", 700); // pass height
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("noFaceDetection", true);
intent.putExtra("output", croppedImageUri);
startActivityForResult(intent, Util.REQUEST_CROP_IMAGE);
} else {
Toast.show(getActivity(), "Please insert memory card to take pictures and make sure it is writeable");
}
} catch (Exception e) {
e.printStackTrace();
}
break;
case Util.REQUEST_CROP_IMAGE:
Logg.e(getClass().getSimpleName(), "Profile_Pic ===== " + selectedPath1);
imgProfile.setImageURI(Uri.parse("file://" + croppedImageUri));
break;
default:
break;
}
}
}

android-image cropping from gallery not working properly

I've an form that user can choose image from gallery or take an image . It works fine when user take an image but it's not working properly when user choose an image from gallery and then I ask for an intent for cropping the image .
On emulator cropping image from gallery works find but on 2 phones that i've tested , when I choose the cropping application , the cropping application crashes or if it not crached , it doesn't work and show the image on imageView .
This is my code :
final int PIC_CROP = 2;
final int CAMERA_CAPTURE = 1;
final int PICK_FROM_FILE = 3;
private Uri picUri;
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE && resultCode == RESULT_OK) {
picUri = data.getData();// get image URI
performCrop();
} else if (requestCode == PIC_CROP) {// crop and compress image
if (resultCode != RESULT_OK)
return;
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
FileOutputStream out = null;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
double width = thePic.getWidth();
double height = thePic.getHeight();
double ratio = 650 / width;
int newheight = (int) (ratio * height);
thePic = Bitmap.createScaledBitmap(thePic, 650, newheight, true);
try {
out = new FileOutputStream(file);
thePic.compress(Bitmap.CompressFormat.JPEG, 295, out);
thePic.compress(Bitmap.CompressFormat.JPEG, 295, bao);
byte[] ba = bao.toByteArray();
switch (whichimg) {
case 1:
simg1 = Base64.encodeBytes(ba);
break;
} catch (Exception e) {
}
}
}
if (item == 0) {// take photo
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
alert.dismiss();
} else if (item == 1) {// photo from gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, (getString(R.string.choosephototo))),
CAMERA_CAPTURE);
alert.dismiss();
}
this is the cropping codes :
private void performCrop() {
try {
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", 356);
//cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
MyToast.makeText(NewAdd2.this, DariGlyphUtils.reshapeText(getString(R.string.devicecouldcop)));
}
}
Could you help me ? Why it is not working fine ?
thanks
Try Below link for your Question, it will Work
may Problem in your performCrop() Method
Link For Crop

Cropping Image in Android (Crop Intent)

I used this code to use android's built in image crop tools. My code is the following
public void takePicture(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null){
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
takePictureIntent.putExtra("crop", "true");
takePictureIntent.putExtra("aspectX", 0);
takePictureIntent.putExtra("aspectY", 0);
takePictureIntent.putExtra("outputX", 200);
takePictureIntent.putExtra("outputY", 150);
takePictureIntent.putExtra("return-data", true);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageViewImage.setImageBitmap(imageBitmap);
}
}
takePicture is called inside a click listener for a Button. What is done is I can open android camera take the picture and when hitting save the image is saved on my imageView. But no cropping activity appears, plus the image on imageView looks awfull. The quality is like it's pixelated. Am I doing something wrong? I used a Samsung galaxy tab 3 to test my app
EDIT using the answer bellow...Stil not working
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Log.d("onActivityResult", "Inside on activity for result");
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageViewImage.setImageBitmap(imageBitmap);
fileUri = getImageUri(this, imageBitmap);χ
cropImage();
}else if (requestCode == REQUEST_IMAGE_CROP && resultCode == RESULT_OK){
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap)extras.get("data");
imageViewImage.setImageBitmap(imageBitmap);
}
}
public void takePicture(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null){
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
public void cropImage() {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(fileUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, REQUEST_IMAGE_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(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
LocCat here
You can try this.
private void doCrop(Uri picUri) {
try {
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", 128);
cropIntent.putExtra("outputY", 128);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CROP_PIC_REQUEST_CODE);
}
// 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(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Get Uri from bitmap
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
Declare
final int CROP_PIC_REQUEST_CODE = 1;
Than simply
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CROP_PIC_REQUEST_CODE) {
if (data != null) {
Bundle extras = data.getExtras();
Bitmap bitmap= extras.getParcelable("data");
yourImageView.setImageBitmap(bitmap);
}
}
}

Crop Images issues with Capture Image using Android

I am using Camera Function to Capture Images 2 times from different buttons. After Captured Image, It goes to Crop Option where user can crop the Image. It is also working fine.
But, now issue is that when user capture second image then App redirects to Crop image and show first image only rather than second capture image. I am also deleting image after It has been set to ImageView. Don't know what is the wrong ?
Please Help me to solve this issue.
My Code :
Bitmap bm_PhotoProof = null;
Bitmap bm_AddressProof = null;
private static final String TEMP_PHOTO_FILE = "tmp_ihis.jpg";
private static final int REQ_CODE_PICK_IMAGE_PHOTOPROOF = 0;
private static final int REQ_CODE_PICK_IMAGE_ADDRESSPROOF = 1;
imgPhotoProof.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
cameraIntent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(cameraIntent,
REQ_CODE_PICK_IMAGE_PHOTOPROOF);
}
});
imgAddressProof.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
cameraIntent.putExtra("outputFormat",
Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(cameraIntent,
REQ_CODE_PICK_IMAGE_ADDRESSPROOF);
}
});
private Uri getTempUri() {
return Uri.fromFile(getTempFile());
}
private File getTempFile() {
File f = new File(Environment.getExternalStorageDirectory(),
TEMP_PHOTO_FILE);
try {
f.createNewFile();
} catch (IOException e) {
Log.e("getTempFile()->", e.getMessage().toString());
}
return f;
}
public void cropCapturedImage(Uri picUri, String Type) {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 0);
cropIntent.putExtra("aspectY", 0);
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
cropIntent.putExtra("return-data", true);
if (Type.equals("Photo")) {
startActivityForResult(cropIntent, 5);
} else {
startActivityForResult(cropIntent, 6);
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQ_CODE_PICK_IMAGE_PHOTOPROOF) {
File tempFile = getTempFile();
cropCapturedImage(Uri.fromFile(tempFile), "Photo");
} else if (requestCode == REQ_CODE_PICK_IMAGE_ADDRESSPROOF) {
File tempFile = getTempFile();
cropCapturedImage(Uri.fromFile(tempFile), "Address");
}
if (resultCode != Activity.RESULT_CANCELED) {
if (requestCode == 5) {
if (data != null) {
Bundle extras = data.getExtras();
bm_PhotoProof = extras.getParcelable("data");
}
imgPhotoProof_Pic.setImageBitmap(bm_PhotoProof);
File tempFile = getTempFile();
if (tempFile.exists()) {
tempFile.delete();
}
}
if (requestCode == 6) {
if (data != null) {
Bundle extras = data.getExtras();
bm_AddressProof = extras.getParcelable("data");
}
imgAddressProof_Pic.setImageBitmap(bm_AddressProof);
File tempFile = getTempFile();
if (tempFile.exists()) {
tempFile.delete();
}
}
}
}
}
I have solved this issue.Before, i was deleting just file. Now, I have also cleared cached and it solved my problem.
File tempFile = getTempFile();
if (tempFile.exists()) {
tempFile.delete();
}
to
File tempFile = getTempFile();
File cacheDir = getActivity().getCacheDir();
File file = new File(cacheDir, getTempFile().toString());
file.delete();

Categories

Resources