I am trying to provide crop facility to image picked from gallery ,it works fine except Android 4.4.How to solve this problem? i am using following code
This is how i am calling gallery intent based on Android version
ImageView ivGallery = (ImageView) pop.findViewById(R.id.ivGallery);
ivGallery.setOnClickListener(new OnClickListener()
{
#SuppressLint("InlinedApi")
#Override
public void onClick(View v)
{
if (Build.VERSION.SDK_INT < 19)
{
Intent intent = new Intent();
pop.dismiss();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("return-data", true);
intent.putExtra("aspectX", 300);
intent.putExtra("aspectY", 300);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
startActivityForResult(intent, StaticMembers.galleryRequestCode); //1=gallery
}
else
{
picUri = ImageUtils.getTempUri();
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
intent.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
pop.dismiss();
startActivityForResult(intent, StaticMembers.GALLERY_KITKAT_INTENT_CALLED);
}
}
});
This is my onActivityResult
#SuppressLint("NewApi")
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == StaticMembers.galleryRequestCode && resultCode == Activity.RESULT_OK)
{
Utils.deleteTempFolder();
Bundle extras = data.getExtras();
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
bmp = extras.getParcelable("data");
thumbBitmap = Bitmap.createScaledBitmap(bmp, 100, 100, true);
imgdp.setImageBitmap(bmp);
setConfirmPicDialog();
}
else if (requestCode == StaticMembers.GALLERY_KITKAT_INTENT_CALLED && resultCode == Activity.RESULT_OK)
{
Log.d("kitkat", "Inside onActivity result for kitkat");
picUri = data.getData();
performCrop(); //what is to be done here?
}
}
private void performCrop()
{
try
{
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picUri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("return-data", true);
cropIntent.putExtra("aspectX", 300);
cropIntent.putExtra("aspectY", 300);
cropIntent.putExtra("outputX", 300);
cropIntent.putExtra("outputY", 300);
startActivityForResult(cropIntent, StaticMembers.galleryRequestCode);
}
catch (ActivityNotFoundException anfe)
{
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(UserInfoActivity.this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
What changes should be done in above code in order to have crop intent in Android 4.4
It is not a good practice to use Gallery's crop functionality. It is missing in some firmwares, so app may even crash in some cases.
Instead you can use this library android-cropimage
Related
I want to capture profile image from camera and crop. In our code camera image is working but cropping is not working.
Camera method
private void cameraIntent() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
outputFileUri = ProviderUtil.getOutputMediaFileUri(getActivity().getBaseContext());
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, REQUEST_CAMERA);
}
}
Crop Image method
public void cropCapturedImage() {
try {
getActivity().grantUriPermission("com.ht.msb.mysocialbuy.provider",outputFileUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(outputFileUri, "image/*");
CropIntent.putExtra("crop", "true");
if (imagebrowseType == 1) {
CropIntent.putExtra("outputX", 400);
CropIntent.putExtra("aspectX", 1);
CropIntent.putExtra("aspectY", 1);
} else {
CropIntent.putExtra("outputX", 600);
CropIntent.putExtra("aspectX", 6);
CropIntent.putExtra("aspectY", 4);
}
CropIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
CropIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
CropIntent.putExtra("outputY", 400);
CropIntent.putExtra("scaleUpIfNeeded", true);
CropIntent.putExtra("return-data", true);
CropIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(CropIntent, PIC_CROP);
} catch (ActivityNotFoundException e) {
Log.e("error_crop",e.toString()+" 1");
}
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
if (outputFileUri != null) {
cropCapturedImage();
}
} else if (requestCode == PIC_CROP) {
try {
Bitmap thePic;
if (data != null) {
thePic = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), outputFileUri);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thePic.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
}
} catch (Exception e) {
Log.e("error", e.toString() + "");
}
}
}
}
Android does not have an image-cropping Intent.
You should add a library to your project that handles image cropping, then use that library with the image that you get back from your startActivityForResult() call.
in onActvityResult method change your code like:
if (requestCode == PIC_CROP) {
if (data != null) {
// get the returned data
// get the cropped bitmap
if (data.getExtras()!=null) {
Bundle extras = data.getExtras();
Bitmap selectedBitmap = extras.getParcelable("data");
imgPhoto.setImageBitmap(selectedBitmap);
}
}
}
Or, Use uCrop library which makes things easier.
Aim :my application take images from camera and gallery.And crop this image and save to external storage.
Issue : Application shows unfortunately photos has stooped message while clicking save button from crop page.The error happens only in marshmallow devices.working fine in all other devices.
my code to take image is given below:
final CharSequence[] options = {"Take Photo", "Choose from Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(CategoryActivity.this);
builder.setTitle("Select Pic Using...");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo")) {
try {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
pic = new File(Environment.getExternalStorageDirectory(),
"tmp_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
picUri = Uri.fromFile(pic);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, picUri);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, LOAD_IMAGE_CAMERA);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
} else if (options[item].equals("Choose from Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), LOAD_IMAGE_GALLARY);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("profile", false);
editor.commit();
// finish();
}
}
});
builder.show();
}
});
if (requestCode == LOAD_IMAGE_CAMERA && resultCode == RESULT_OK) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("profile", false);
editor.commit();
Intent in = new Intent(CategoryActivity.this, ImageCrop.class);
in.putExtra("URI", picUri);
in.putExtra("cat", type);
in.putExtra("contest_idadd", contestid_i);
startActivity(in);
} else if (requestCode == LOAD_IMAGE_GALLARY) {
if (data != null) {
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("profile", false);
editor.commit();
picUri = data.getData();
Intent in = new Intent(CategoryActivity.this, ImageCrop.class);
in.putExtra("URI", picUri);
in.putExtra("cat", type);
in.putExtra("contest_idadd", contestid_i);
startActivity(in);
}
}
And crop using the below code
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
picUri = (Uri) bundle.get("URI");
cat_value = (String) bundle.get("cat");
}
protected void CropImage() {
try {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(picUri, "image/*");
Log.d("piccccc",picUri+"");
intent.putExtra("crop", "true");
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 3);
intent.putExtra("aspectY", 3);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, CROP_IMAGE);
} catch (ActivityNotFoundException e) {
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CROP_IMAGE) {
if (data != null) {
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap photo = extras.getParcelable("data");
image_array.add(photo);
crop.setImageBitmap(photo);
if (pic != null) {
if (pic.delete()) {
}
}
}
else
{
Log.d("cropppppppppppppp", requestCode + "");
}
}
In marshmallow devices images not found in saved external storage path.Anyone please give me a solution.
Add proper implementation of taking
android.permission.WRITE_EXTERNAL_STORAGE
permission in your code.
You need to handle permissions in your code from marshmallow and above:
http://developer.android.com/training/permissions/index.html
http://developer.android.com/training/permissions/requesting.html
Or build your app with SDK 22 or below, TargetSdkVersion = 22
When I click on the button, It takes me to gallery to select an image, but it returns back to the activity without cropping, as soon as I select an image. Also the image is not displayed in the imageView.
Here's my code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
imgview = (ImageView) findViewById(R.id.imageView1);
Button buttonCamera = (Button) findViewById(R.id.btn_take_camera);
Button buttonGallery = (Button) findViewById(R.id.btn_select_gallery);
buttonCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
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) {
}
}
});
buttonGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
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.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
imgview.setImageBitmap(photo);
}
}
if (requestCode == PICK_FROM_GALLERY) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Bitmap photo = extras2.getParcelable("data");
imgview.setImageBitmap(photo);
}
}
}
}
but it returns back to the activity without cropping
That is because there is no requirement for any activity anywhere to honor those random Intent extras, let alone whatever activity the user chooses for your particular request (based on their device and installed ACTION_IMAGE_CAPTURE and ACTION_GET_CONTENT activities). You will note that none of those extras are documented on ACTION_IMAGE_CAPTURE or ACTION_GET_CONTENT.
There are many image cropping libraries available for Android. Use one.
Also the image is not displayed in the imageView.
That is because ACTION_GET_CONTENT does not return anything via a data Intent extra. Use the Uri that ACTION_GET_CONTENT returns (obtained via getData() on the Intent passed into onActivityResult()). Provide that Uri to your favorite image-loading library (Picasso, Universal Image Loader, etc.).
When i try to use camera and get that images and try to crop using cropintent
but after returning from CropIntent In OnActivityResult the intent value is showing null
On the Dialog i use camera the dialog code is here
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File file=null;
try {
file = createImageFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
Log.e("File creation","Error occured during file creation");
e1.printStackTrace();
}
intent.putExtra("return-data", false);
startActivityForResult(intent, PICK_FROM_CAMERA);
dialog.dismiss();
the OnActivityReultCode is
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
String path="";
switch (requestCode) {
case PICK_FROM_CAMERA:
if(resultCode==RESULT_OK && !currentPhotopath.isEmpty()){
mImageCaptureUri = intent.getData();
Log.e("Camera path","" +path);
Log.e("Camera uri",mImageCaptureUri.toString());*/
performCrop();
}
else{
Toast.makeText(getApplicationContext(),"Error occured",Toast.LENGTH_SHORT).show();
}
break;
case PIC_CROP:
Bundle extras = intent.getExtras();
Bitmap bp = extras.getParcelable("data");
photoView.setImageBitmap(bitmap);
break;
default:
Toast.makeText(getApplicationContext(),"Could not load image", Toast.LENGTH_SHORT).show();
break;
}
super.onActivityResult(requestCode, resultCode, intent);
}
And the performCrop method is
private void performCrop()
{
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(mImageCaptureUri, "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 );
}
There is times that there is issues with the launchMode. Try use:
android:launchMode="singleTop"
I have implemented code for both image capturing and cropping functionality.
Same is working in Lenovo A536 and Huwaii Honor Holly but not working in Huwaii Ascend P7-L10. Even all of them have android 4.4.2 with Loilipop update.
First issue is that caputred image is not getting stored on the sd card while I have already provided permission for write access.
Second is I am not able to implement crop view in square form. It is adjustable by the end user while selecting image from gallery in only the phone above mentioned. My code is :
public void selectGiniPicture(View view) {
isGiniImageSelected = true;
final CharSequence[] options = { "Take Picture", "Choose from Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(WorkerRegActivity.this);
builder.setTitle("Select Your Picture!");
builder.setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Picture"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
else if (options[item].equals("Choose from Gallery"))
{
Intent intent = new Intent();
//Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
//intent.putExtra("aspectX", 0);
//intent.putExtra("aspectY", 0);
intent.putExtra("outputX", 225);
intent.putExtra("outputY", 225);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
Perform Crop function for camera
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", 0);
//cropIntent.putExtra("aspectY", 0);
// indicate output X and Y
cropIntent.putExtra("outputX", 225);
cropIntent.putExtra("outputY", 225);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 6);
}
// 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();
}
}
OnActivityResult Function is as:
if (resultCode == RESULT_OK)
{
if (requestCode == 1)
{
picUri = data.getData();
performCrop();
}
else if (requestCode == 2) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
capturedImage.setImageBitmap(photo);
}
}
}