I'm trying to implement a function to pick a photo from android built-in gallery (not from SD card) or taking a photo using camera. After the image is picked it will be cropped by the user. An ImageView will also be updated with the cropped image. Now after picking an image from gallery, I cannot crop it -- "No Activity found to handle Intent act=com.android.camera.action.CROP" error was returned. And I also can't seem to update the ImageView with the selected image. Can someone please tell me what was wrong? Thanks.
My code is as follows:
private void ShowPickDialog() {
new AlertDialog.Builder(this)
.setTitle("Add Photo")
.setNegativeButton("Select from Photos", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),3);
}
})
.setPositiveButton("Take a Pic", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri
.fromFile(new File(Environment.DIRECTORY_PICTURES,
"image.jpg")));
startActivityForResult(intent, 2);
}
}).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
startPhotoZoom(data.getData());
break;
case 2:
File temp = new File(Environment.getExternalStorageDirectory()
+ "/test.jpg");
startPhotoZoom(Uri.fromFile(temp));
break;
case 3:
if(data != null){
setPicToView(data);
}
break;
default:
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
public void startPhotoZoom(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 150);
intent.putExtra("outputY", 150);
intent.putExtra("return-data", true);
startActivityForResult(intent, 3);
}
private void setPicToView(Intent picdata) {
Bundle extras = picdata.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
//update ImageView with selected image
mPic.setImageBitmap(photo);
}
}
Android Does Not Have a Crop Intent
you can use libraries instead
Android CropImage
Related
i was adding profile picture option in my application from camera and gallery All was working well till i tried to select a picture and display it in my image circulerView .when i select a picture to set in my imageCirculerView , it doesn't set in the imageCirculerView and no error is even showing ..So i literally don't know where i did the mistake
private void pickFromGallery() {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"select picture"), IMAGE_PICK_GALLERY_CODE);
}
private void pickFromCamera() {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Images.Media.TITLE, "Temp_image Title");
contentValues.put(MediaStore.Images.Media.DESCRIPTION, "Temp_desc Desctription");
image_uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, image_uri);
startActivityForResult(intent, IMAGE_PICK_CAMERA_CODE);
}
profilepicture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pick a image
showImagePickDialog();
}
});
private void showImagePickDialog() {
//options to display in dialoge
String[] options = {"Camera", "Dialoge"};
//dialoge
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick Image").setItems(options, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
//Camera Clicked
if (checkCameraPermission()) {
//Camera request allowed
pickFromCamera();
} else {
//not allowed Request
requestCameraPermission();
}
} else {
//gallery Clicked
if (checkStoragePermission()) {
//Storage request allowed
pickFromGallery();
} else {
//not allowed Request
requestStoragePermission();
}
}
}
}).show();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if(requestCode == RESULT_OK){
if(requestCode== IMAGE_PICK_GALLERY_CODE){
//getPicked image
image_uri = data.getData();
//settoimageView
profilepicture.setImageURI(image_uri);
}else if(requestCode== IMAGE_PICK_CAMERA_CODE){
//settoimageView
profilepicture.setImageURI(image_uri);}
}
super.onActivityResult(requestCode, resultCode, data);
}
Here is my code. I'm having problems with showing and saving the pic picked from the user's gallery, whilst when I do it from the camera it works just fine for me.
here is how the user is reffered to his gallery-
if (v.getId() == R.id.btnAddDucuments) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setMessage("to add documents");
alertDialog.setPositiveButton("from camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// user choose to take a picture
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAM_REQUEST);
}
});
alertDialog.setNegativeButton("from gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// user choose to pick a photo from gallery
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), GET_FROM_GALLERY); }
});
alertDialog.show();
and here is how the picture is supposed to be saved. I need it both as a String (so I can use it further on) and in the ImageView that is in this avtivity (named imgTheDocument).
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
Uri selectedImage = data.getData();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
StringImgDocument = BitMapToString(bitmap);
imgTheDocument.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I run this code the activity stops after pressing on the wanted picture. I can't understand where is the problem. I have used this code before and it worked just fine.
I'll appriciate any help!! Thanks:)
my manifest permissions:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
the intent to pick image(This is Error free and works on all android versions!)
Intent intent;
if (Build.VERSION.SDK_INT < 19){
intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, GET_FROM_GALLERY);
} else {
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, GET_FROM_GALLERY);
}
This is for onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==GET_FROM_GALLERY && resultCode == Activity.RESULT_OK) {
//you can both use the selectedImageUri or yourSelectedImagePath for working the Image you picked,send it somewhere or store it in database or etc..
Uri selectedImageUri = data.getData();
String yourSelectedImagePath = selectedImage.tostring;
//I just use the Uri to display the selected image in an imageview
yourImageView.setImageUri(selectedImage);
}
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
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);
}
}
}
I have 'SherlockFragmentActivity' with overrided 'onActivityResult'.
I try to get image from camera and gallery and crop it.
The problem is I returned on my activity not fragment after onActivityResult called.
...
FragmentTransaction t = fragmentManager.beginTransaction();
LogInFragment logFrag = new LogInFragment();
t.replace(R.id.fragment_container, logFrag);
t.commit();
...
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Activity layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment_container"
android:background="#color/textWhite"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
And I also have 'SherlockFragment' where I picked image:
startImagePickerDialog(this);
public void startImagePickerDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(getSherlockActivity());
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent intent = new Intent();
// call android default gallery
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("noFaceDetection", true);
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), Const.GALLERY_PICTURE);
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "ActivityNotFoundException");
}
}
});
myAlertDialog.setNegativeButton("Camera", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
// call android default camera
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", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("noFaceDetection", true);
try {
intent.putExtra("return-data", true);
startActivityForResult(intent, Const.CAMERA_REQUEST);
} catch (ActivityNotFoundException e) {
Log.e(LOG_TAG, "ActivityNotFoundException");
}
}
});
myAlertDialog.show();
}
And 'onActivityResult' in 'SherlockFragment':
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(LOG_TAG, "onActivityResult");
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != getSherlockActivity().RESULT_OK) {
Log.e(LOG_TAG, "resultCode != RESULT_OK");
return;
}
if (requestCode == Const.CAMERA_REQUEST) {
Log.d(LOG_TAG, "requestCode == CAMERA_REQUEST");
Bundle extras = data.getExtras();
if (extras != null) {
Log.d(LOG_TAG, "extras != null");
Bitmap photo = extras.getParcelable("data");
icon.setImageBitmap(photo);
}
}
if (requestCode == Const.GALLERY_PICTURE) {
Log.d(LOG_TAG, "requestCode == GALLERY_PICTURE");
Bundle extras2 = data.getExtras();
if (extras2 != null) {
Log.d(LOG_TAG, "extras != null");
Bitmap photo = extras2.getParcelable("data");
icon.setImageBitmap(photo);
}
}
}
UPDATE
When I call camera activity my main activity call 'onSaveInstanceState' and after that 'onRestoreInstanceState'. Is it a reason?
Check your "Settings" -> "Developer options" -> "Don't keep activities" flag.
This is the nature of android if your device needs memory it destroys activities which are not visible. So you have to consider that your activity can be recreated any time. BTW "Don't keep activities" option is there to simulate your application when your device needs memory and destroys your backstack activities.
Try Like this
Change
Bitmap photo = extras.getParcelable("data");
To
Bitmap photo=(Bitmap) intent.getExtras().get("data");
Edited:-
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(LOG_TAG, "onActivityResult");
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != getSherlockActivity().RESULT_OK) {
Log.e(LOG_TAG, "resultCode != RESULT_OK");
return;
}
if (requestCode == Const.CAMERA_REQUEST) {
Log.d(LOG_TAG, "requestCode == CAMERA_REQUEST");
Bitmap photo=(Bitmap) intent.getExtras().get("data");// Changed Here
icon.setImageBitmap(photo);
}
if (requestCode == Const.GALLERY_PICTURE) {
Log.d(LOG_TAG, "requestCode == GALLERY_PICTURE");
Uri imageUri= intent.getData();// Changed Here, or first decode the image to Avoid OutOfMemory Error
icon.setImageURI(imageUri);
}
}
Please replace
frag.startActivityForResult(...);
In your fragment with
startActivityForResult(...)