I Currently have it where a user pics an image from gallery. It is then converted to a bitmap and displayed in a imageview and sent to my server. The way I currently have it being cropped is bellow. However, I want the user to have a 300x300 box they can move around and when they press upload it will upload what is within that 300x300 view
if (bitmap.getWidth() >= bitmap.getHeight()){
bitmap = Bitmap.createBitmap(
bitmap,
0,
0,
bitmap.getHeight(),
bitmap.getHeight()
);
}else{
bitmap = Bitmap.createBitmap(
bitmap,
0,
0,
bitmap.getWidth(),
bitmap.getWidth()
);
}
This is my first application so I am still earning the ropes. Thank you for your time and patience ahead of time.
This is a sample function to crop a image of desired size.Please do check it.you can call this 'performCrop()' function from onActivityResult().
public void performCrop() {
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("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 300); //your desired size here
cropIntent.putExtra("outputY", 300); //your desired size here
// retrieve data on return
cropIntent.putExtra("return-data", true);
File f = createNewFile("CROP_");
try {
f.createNewFile();
} catch (IOException ex) {
} catch (NullPointerException ex) {
}
CropImagedUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, CropImagedUri);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 2);
} 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 void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
PerformCrop();
// Img.setImageURI(selectedImageUri);
} else if (requestCode == 2) {
CropImagePath = getRealPathFromURI(CropImagedUri);
}
}
Related
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();
}
}
}
i want crop photo that i take from camera, so far i try do it like this but with no success
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment
.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
getActivity().startActivityForResult(intent, REQUEST_CAMERA);
is it possible to do it without any 3th part libraries.?
i checked https://github.com/biokys/cropimage
but it doesnt gave me any results
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
String path = data.getStringExtra(CropImage.IMAGE_PATH);
// if nothing received
if (path == null) {
return;
}
// cropped bitmap
Bitmap bitmap = BitmapFactory.decodeFile(path);
((ImageView) findViewById(R.id.userpicture)).setImageBitmap(bitmap);
}
is it possible to do it without any 3th part libraries.?
Not reliably. Android does not have a CROP Intent.
i checked https://github.com/biokys/cropimage but it doesnt gave me any results
Perhaps you did not integrate it properly. In addition to the libraries mentioned in my blog post (linked to above), the Android Arsenal has a few options.
try this
private void performCrop(Uri picUri) {
try {
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", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", 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 = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
declare:
final int PIC_CROP = 1;
at top.
In onActivity result method, writ following code:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");
imgView.setImageBitmap(selectedBitmap);
}
}
}
Hi I have this app where I allow user to take image and crop. It works on my phones but not on my Samsung Galaxy Tablet. The "Saving image" dialog just remain and not return to my app's onActivityResult... It does return if I cancel crop[
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1337 && resultCode == -1) {
File fi = new File("/sdcard/tmp");
// get the Uri for the captured image - NEW
try {
mImageCaptureUri = Uri
.parse(android.provider.MediaStore.Images.Media
.insertImage(getContentResolver(),
fi.getAbsolutePath(), null, null));
// Log.i("",String.valueOf(thumbnail.getHeight()));
} catch (Exception ex) {
String errorMessage = "Your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage,
Toast.LENGTH_SHORT);
toast.show();
}
performCrop();
}
if (requestCode == PIC_CROP) {
try {
final TextView imgTv = (TextView) findViewById(R.id.info);
// Bundle extras = data.getExtras();
// thumbnail = extras.getParcelable("data");
Log.i("a", "test");
// NEW
final String filePath = Environment.getExternalStorageDirectory()
+ "/temporary_holder.jpg";
thumbnail = BitmapFactory.decodeFile(filePath);
ImageView image = (ImageView) findViewById(R.id.img);
image.setImageBitmap(thumbnail);
}}
private void performCrop() {
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(mImageCaptureUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
// indicate output X and Y
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 300);
// retrieve data on return
// cropIntent.putExtra("return-data", true);
File f = new File(Environment.getExternalStorageDirectory(),
"/temporary_holder.jpg");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}
uri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
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 doesn't support the crop action!";
Toast toast = Toast
.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
Please refer following links for better solution:
Link 1
Link 2
I have the following code for users to crop image. When I set the size beyond 256, it does not work. My gut feel is "cropIntent.putExtra("return-data", true);" causing the error. How do I pass in the uri to cropIIntent and retrieve out from onActivityResults? In another words, save the image after crop and retrieve.
private void performCrop() {
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(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
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 doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
try {
final TextView imgTv = (TextView) findViewById(R.id.imageInfo);
Bundle extras = data.getExtras();
thumbnail = extras.getParcelable("data");
ImageView image = (ImageView) findViewById(R.id.pestImage);
image.setImageBitmap(thumbnail);
File f = new File(mImageCaptureUri.getPath());
if (f.exists()) {
f.delete();
}
}
}//end onactivity results
private void performCrop() {
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(mImageCaptureUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 4);
cropIntent.putExtra("aspectY", 3);
//indicate output X and Y
cropIntent.putExtra("outputX", 800);
cropIntent.putExtra("outputY", 800);
File f = new File(Environment.getExternalStorageDirectory(),
"/temporary_holder.jpg");
try {
f.createNewFile();
} catch (IOException ex) {
Log.e("io", ex.getMessage());
}
uri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
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 doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PIC_CROP) {
String filePath = Environment.getExternalStorageDirectory()
+ "/temporary_holder.jpg";
thumbnail = BitmapFactory.decodeFile(filePath);
//thumbnail = BitmapFactory.decodeFile(filePath);
// Log.i("",String.valueOf(thumbnail.getHeight()));
ImageView image = (ImageView) findViewById(R.id.pestImage);
image.setImageBitmap(thumbnail);
}}
Try
Uri cropedImageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(cropedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
Now filePath is the path to cropped image
To load bitmap from filePath Use
private Bitmap loadImageFromSDCard(String filePath) {
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 1;
bfo.outWidth = 100;
bfo.outHeight = 100;
Bitmap photo = BitmapFactory.decodeFile(filePath, bfo);
return photo;
}
if (android.os.Build.VERSION.SDK_INT > 10){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
int gcd = BigInteger.valueOf(ImageWidth).
gcd(BigInteger.valueOf(ImageHeight)).intValue();
cropIntent.putExtra("aspectX", (ImageWidth / gcd));
cropIntent.putExtra("aspectY", (ImageHeight / gcd));
cropIntent.putExtra("outputX", ImageWidth); // X
cropIntent.putExtra("outputY", ImageHeight); // Y
cropIntent.putExtra("return-data", true);
cropIntent.setData(picUri);
startActivityForResult(cropIntent, PIC_CROP_INTENT_ID);
}
I'm trying to capture a photo, then crop it. So basically, I start camera Intent, then I start crop intent.
As I can't pass a big bitmap through "onActivityResult" data, I created a URI to save the image directly to SDCard, but for some reason, it's being saved on the emulator, but not on a real device.
(I mean, the CROP isn't saved on the real device. )
This is the code:
File resultingFile;
Bitmap fotoCreada;
Uri uriFoto;
Camera Intent:
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/PictureFolder/");
folder.mkdirs();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile=new File(folder.toString() + "/"+currentDateandTime+".jpg");
this.uriFoto=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, this.uriFoto);
startActivityForResult(cameraIntent, 1888);
Then I retrieve result:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//Si venim de la camera
if( requestCode == 1888 && resultCode == -1) { //-1 = TOT HA ANAT BE.
Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
Matrix matrix = new Matrix();
matrix.postRotate(90);
matrix.postScale(0.5f,0.5f);
Bitmap photoRotated = Bitmap.createBitmap(photo, 0, 0, photo.getWidth(), photo.getHeight(), matrix, true);
//Desem foto rotada.
this.fotoCreada=photoRotated;
try {
FileOutputStream out = new FileOutputStream(this.resultingFile.getPath());
this.fotoCreada.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
performCrop();
}
So I call performCrop();
private void performCrop() {
try {
Log.d("debugging","Estic a crop. La foto és:"+this.uriFoto);
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(this.uriFoto, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("outputX", 600);
cropIntent.putExtra("outputY", 600);
//cropIntent.putExtra("return-data", true);
cropIntent.putExtra("ouput", this.uriFoto);
startActivityForResult(cropIntent,1234);
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this.cont, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
As you can see:
cropIntent.putExtra("ouput", this.uriFoto);
Basically tells to Output the crop to that URI.
The URI is correct, as I've debugged it (LogCat):
Estic a crop. La foto és: file:///mnt/sdcard/PictureFolder/20130123_223058.jpg
And then, I'm back to activity result:
[... previous onactivityresult]
if (requestCode == 1234 && resultCode == -1){
Log.d("debugging","Ha acabat el croop.");
Uri imageUri = this.uriFoto;
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(this.cont.getContentResolver(), imageUri);
this.fotoCreada=photo;
((ImageView) myFragmentView.findViewById(R.id.fotoCapturada)).setImageBitmap(this.fotoCreada);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//Bitmap photo = BitmapFactory.decodeFile(resultingFile.getPath());
}
But what I end up with, is displaying (setImageBitmap(this.fotoCreada);) the captured image from camera, but not the cropped picture. This happens on real device, but not on Emulator!
I tried to explain the best way I could. Feel free to ask.
Thanks.