I'm working on some code someone else wrote and I'm having trouble handling the result of a camera intent.
Basically i have a DashBoardActivity which contains a tab with a fragment called "MyProfileContainer", which contains a "SettingsFragment" fragment which contains a "EditProfileFragment"fragment.
In the EditProfileFragment the user can take a picture for his profile. It works but it calls the onActionResult on the Dashboard Activity.
I read some guide on how to redirect it to the EditProfileFragment but I haven't been able to do it.
I'm losing literally days on this one and I can't figure it out.
This is onActivityResult on the Dashboard Activity
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
And this is EditProfileFragment
private Uri imageUri = null;
public void captureImage() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
getParentFragment().startActivityForResult(intent, 100);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 100 && resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getActivity().getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getActivity().getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
profilePhoto.setImageBitmap(bitmap);
Toast.makeText(getActivity(), selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getActivity(), "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
I don't know if I have to override the method on every class between this two or if I'm doing something else wrong, but I'm sure the EditProfileFragment onActivityResult is never called.
I found out the problem, and it was actually a bug of Android.
The fragment that was to recieve the result was a fragment nested inside other fragment and the method call was not properly propagated that deeply.
I had to override the method on the containing fragment manually and it worked.
The easy trick to call OnActivityResult in nested fragment.
1) Add this code in your captureImage method it will start a new activity.
Intent intent = new Intent(getContext(), CameraPreviewActivity.class);
startActivityForResult(intent, 111);
2) Now CameraPreviewActivity activity will open and it will start camera activity and returns the result to fragment.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, FragmentAccPhoto.REQUEST_IMAGE_CAPTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
setResult(RESULT_OK, data);
finish();
}
Related
This question already has answers here:
onActivityResult is not being called in Fragment
(41 answers)
Closed 5 years ago.
I am using FragmentPagerAdapter with three tabs. Each tab is having multiple fragments, in one of these Fragment i request to get photo from Gallery or Camera. The issue is onActivityResult() of the parent Activity hosting the tab is invoked not that of the Fragment. Help Appreciated.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case MEDIA_TYPE_IMAGE_FROM_GALLERY:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String realPath = getImageRealPath(selectedImage);
realPaths.add(realPath);
Glide.with(mActivity)
.load(realPath)
.into(profile_imv);
}
break;
case MEDIA_TYPE_IMAGE_BY_CAMERA:
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String realPath = getImageRealPath(selectedImage);
realPaths.add(realPath);
Glide.with(mActivity)
.load(realPath)
.into(profile_imv);
}
break;
}
}
Here is how i call the Intent both for Gallery and Camera
public void choosePhotoFromGallary() {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, MEDIA_TYPE_IMAGE_FROM_GALLERY);
}
private void takePhotoFromCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(mActivity.getPackageManager()) != null) {
startActivityForResult(intent, MEDIA_TYPE_IMAGE_BY_CAMERA);
}
}
This is the MainACtivity onActivityResult() code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Fragment fragment = getSupportFragmentManager().findFragmentByTag("USER_DETAILS");
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Change getActivity().startActivityForResult(..) to startActivityForResult(..) this will pass the result intent to the fragment's onActivityResult(..)
Beautifully explained in this SO post here
Override onActiviyResult() inside the fragment and re-write the code there.
//As per some suggestions I write this code in my project but still going to face same issue
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode == Activity.RESULT_OK)
{
Fragment yourFragment = getSupportFragmentManager().findFragmentById(R.id.frame_container); // same tag while adding fragment for the first time.
if (yourFragment != null)
{
yourFragment.onActivityResult(requestCode, resultCode, data); //calling method that should be defined in your fragment.
}
}
}
//this code is from MainActivity.java class..
//Should I write Above Activity Result Code into my Mainfragment class?
gallery.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
Intent gintent = new Intent();
gintent.setType("image/*");
gintent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
gintent, "Select Picture"), PICK_IMAGE);
} catch (Exception e) {
Toast.makeText(mainActivity,
e.getMessage(), Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
d.dismiss();
}
});
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// define the file-name to save photo taken by Camera
// activity
String fileName = "new-photo-name.jpg";
// create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");
// imageUri is the current activity attribute, define
// and save it for later usage (also in
// onSaveInstanceState)
imageUri = getActivity().getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
// create new Intent
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, PICK_Camera_IMAGE);
}
});
// This is the code belongs to my Profile Fragment class..
// I stuck over here didn't get any kind of solution
// I'm using fragment viewpager
Here you done mistake if do like this
**PICK_IMAGE/PICK_Camera_IMAGE**
it will divide your request codes and resulting code will be different
if(requestCode == PICK_IMAGE/PICK_Camera_IMAGE && resultCode ==Activity.RESULT_OK)
{
//...
}
Use this code instead of older
if(requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK)
{
//...
}else if(requestCode == PICK_Camera_IMAGE && resultCode ==Activity.RESULT_OK)
{
//...
}
To get the result in your fragment make sure you call
startActivityForResult(intent, PICK_Camera_IMAGE);
instead of
getActivity().startActivityForResult(intent, PICK_Camera_IMAGE);
inside your fragment.
As all fragments are invoked into an activity so the result will have to be transferred into the fragments from onActivityResult of your activity just like you are doing.
You will have to create you own onActivityResult method in your fragment with same signature as well to update your fragment UI.
remove this
super.onActivityResult(requestCode, resultCode, data);
I have a MainActivity and one attached sliding menu to it.
I am calling Fragments separately when the user clicks a button in the sliding menu.
After that, inside one of these Fragments, I am calling a gallery photo chooser and I need to handle it inside the Fragment.
How can I solve this problem?
Override onActivityResult method in fragment and call it from Activity's onActivityResult method by making object of that fragment like following way,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
I hope it will help you.
You can use this code
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, SELECT_REQUEST);
and within onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_REQUEST && resultCode == Activity.RESULT_OK) {
try {
InputStream stream = getActivity().getContentResolver()
.openInputStream(data.getData());
pic = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(pic);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I've got a code that starts intent to take picture from camera
Intent pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent, PhotoUtility.CAMERA_PICTURE);
And after picture is taking back it should calls onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == PhotoUtility.CAMERA_PICTURE) {
File f = PhotoUtility.saveFromCamera(data, picIV);
imagePath = f.getAbsolutePath();
} else if (requestCode == PhotoUtility.GALLERY_PICTURE) {
File f = PhotoUtility.saveFromGallery(data, getActivity(), picIV);
imagePath = f.getAbsolutePath();
}
}
With simple Fragment it works fine, but if I use DialogFragment onActivityResult is not calling. How can solve this problem using DialogFragment?
I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}