I want to capture an image and crop it, then with retrofit send this file to the server.
I try this code:
intent open camera
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(Environment.getExternalStorageDirectory(), "POST_IMAGE.jpg");
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
picUri = Uri.fromFile(f);
startActivityForResult(chooserIntent, REQUEST_CAMERA_IMAGE);
onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA_IMAGE) {
path = data.getData();
performCrop(path);
}
}
and the performCrop method
private void performCrop(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", 300);
cropIntent.putExtra("outputY", 300);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, PIC_CROP);
} catch (ActivityNotFoundException anfe) {
}
}
but this code is wrong! how to do this?
if I save the captured image, then crop it from the gallery, it worked? how to do this?
thank for your attention.
Related
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);
}
}
}
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");
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);
}
}
}
I have this code to retrieve an image from the gallery
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 0);
intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 360);
intent.putExtra("outputY", 360);
try
{
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent, "Complete action using"), req_code);
}
catch(ActivityNotFoundException e)
{
// Do nothing for now
}
But even with the intent.putExtra("crop", "true");, after choosing the image, it won't display any crop activity or whatever... Why?
Because it is not supposed to. Just because you put random extras on random Intent objects does not magically force third party apps to do things that they do not do.
Here is the documentation for ACTION_GET_CONTENT. Note that none of the extras that you list there are in the documentation. Hence, no third party app is necessarily going to expect those extras.
Android does not have a built-in image-cropping capability available to developers. There are plenty of image-cropping libraries available, though.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
try this code :
(For getting image from gallary)
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
dialog.dismiss();
(After picked image from gallary call this )
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bmp = null;
if (resultCode == RESULT_OK) {
try {
if (requestCode == GALLERY_REQUEST) {
// Gallery request result
mImageCaptureUri = data.getData();
TEMP_PHOTO_FILE_NAME = new File(
startCropImage();
} catch (Exception e) {
e.printStackTrace();
File f = new File(getRealPathFromURI(mImageCaptureUri));
if (f.getName().startsWith(FILE_NAME)) {
if (f.exists())
f.delete();
}
}
}
}
private void startCropImage() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
/**
* Check if there is image cropper app installed.
*/
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
intent, 0);
int size = list.size();
/**
* If there is no image cropper app, display warning message
*/
if (size == 0) {
Toast.makeText(this, "Can not find image crop app",
Toast.LENGTH_SHORT).show();
return;
} else {
/**
* Specify the image path, crop dimension and scale
*/
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
/**
* There is posibility when more than one image cropper app exist,
* so we have to check for it first. If there is only one app, open
* then app.
*/
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
}
}
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO: //Select photo == 1
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = imageReturnedIntent.getData();
Bitmap selectedImage = BitmapFactory
.decodeStream(getContentResolver().openInputStream(
imageUri));
*I am working on an application in which i have to take image,crop it and then proceed to further implementation.When i capture image and save it to sdcard it's quality is good but when i try to save image after cropping,It's quality is worst.
Below is my code
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.captureimage) {
try {
//use standard intent to capture an image
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_CAPTURE);
}
catch(Exception e){ String errorMessage = "your device doesn't support capturing images!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_CAPTURE) {
Uri picUri = data.getData();
performCrop(picUri);
}
else if(requestCode == PIC_CROP){this.finish();
Intent i=new Intent(getApplicationContext(),display.class);
i.putExtra("jao", "jao");
startActivity(i);}
else {
Log.v("", "User cancelled");
}
}private void performCrop(Uri picUri) {
// TODO Auto-generated method stub
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);
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
Please guide me how can i make my cropped image quality better because with bad quality image i cannot do my other tasks....