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"
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.
I'm trying to take a picture in gallery, so i know do this in Activity, i use a Intent to call the gallery, and onActivityResult for take the path, but when i use a Fragment, i cannot to use "onActivityResult", can someone give a example of it using a Fragment and CustomDialog?
Inside your fragment write this code
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// ******** code for crop image
i.putExtra("crop", "true");
i.putExtra("aspectX", 100);
i.putExtra("aspectY", 100);
i.putExtra("outputX", 256);
i.putExtra("outputY", 356);
try {
i.putExtra("return-data", true);
startActivityForResult(
Intent.createChooser(i, "Select Picture"), 0);
}catch (ActivityNotFoundException ex){
ex.printStackTrace();
}
In you Main Activity of the fragment write this code onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0 && resultCode == Activity.RESULT_OK){
try {
Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");
img_user.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Try with this, it should work. Don't forget to accept the answer if correct.
I have a fragment where I am calling getActivity().startActivityForResult for camera activity and I have onActivityResult in my MainActivity to handle the Result.
Fragment
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
try {
intent.putExtra("return-data", true);
getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
MainActivity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) //CANCELED
{
Toast.makeText(this, "canceled", Toast.LENGTH_SHORT).show();
}
switch (requestCode) {
case PICK_FROM_GALLERY:
Toast.makeText(this, "Pick from Gallery", Toast.LENGTH_SHORT).show();
if (resultCode == RESULT_OK) {
Toast.makeText(this, "Result Okay", Toast.LENGTH_SHORT).show();
Bundle extras2 = data.getExtras();
if (extras2 != null) {
//Doesn't enter here
} else {
Toast.makeText(this, "extra is null", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
Activity #Oncreate open camera intent
// Camera Option Clicked
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, 1);
Handle onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if(resultCode == RESULT_OK){
if (data != null) {
takePhoto(data);
}
}
break;
}
}
Display image on ImageView
private void takePhoto(Intent imageData){
Bundle extras = imageData.getExtras();
if(extras != null){
imageView.setImageBitmap((Bitmap) extras.get("data"));
}
}
change this line
getActivity().startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
to
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY);
refer this for further information onActivityResult is not being called in Fragment
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
112);
if (requestCode == 112) {
try {
InputStream inputStream = getContentResolver()
.openInputStream(data.getData());
FileOutputStream fileOutputStream = new FileOutputStream(
mFileTemp);
copyStream(inputStream, fileOutputStream);// do other stuff
fileOutputStream.close();
inputStream.close();
//do other stuff
} catch (Exception e) {
e.printStackTrace();
}
}
You should call
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_GALLERY); from your fragment and then implement the onActivityResult() in your Fragment itself, and in the onActivityResult() just check for the result code as
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == PICK_FROM_GALLERY) {
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
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(...)