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();
}
}
}
Related
I have an App with 2 Activities. Activity A (parent) starts Activity B (child) by calling:
startActivityForResult(intentB,B_Request);
Activity B is a barcode scanner. (I use this library: com.journeyapps:zxing-android-embedded:3.2.0#aar).
In this activity I have to override the onActivityResult() method in order to get the scanned code. After that I want to send the data stored in 'code' back to the parent (Activity A) onActivityResult(). I did this like this:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
String code = null;
if(result != null){
if(result.getContents() == null){
return;
}
else {
code = result.getContents();
}
}
else {
super.onActivityResult(requestCode, resultCode, data);
}
Intent intent = new Intent(getApplicationContext(),A.class);
intent.putExtra("scannedCode", code);
setResult(RESULT_OK, intent);
finish();
}
The parent onActivityResult() looks like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == CAMERA_REQUEST){
if(resultCode == RESULT_OK){
String scannedCode = data.getStringExtra("scannedCode");
toastMessage("Scanned code: " + scannedCode);
}
}
}
I get the following exception and instead of seeing Activity A, I'm redirected to the Activitie's A parent.
E/ActivityManager: Failed to schedule configuration change
android.os.DeadObjectException
at android.os.BinderProxy.transactNative(Native Method)
at android.os.BinderProxy.transact(Binder.java:1177)
at android.app.IApplicationThread$Stub$Proxy.scheduleTransaction(IApplicationThread.java:1815)
at android.app.servertransaction.ClientTransaction.schedule(ClientTransaction.java:129)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:47)
at com.android.server.am.ClientLifecycleManager.scheduleTransaction(ClientLifecycleManager.java:100)
at com.android.server.am.ActivityManagerService.updateGlobalConfigurationLocked(ActivityManagerService.java:24782)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24902)
at com.android.server.am.ActivityManagerService.updateDisplayOverrideConfigurationLocked(ActivityManagerService.java:24879)
at com.android.server.am.ActivityStackSupervisor.ensureVisibilityAndConfig(ActivityStackSupervisor.java:1671)
at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1420)
at com.android.server.am.ActivityStackSupervisor.startSpecificActivityLocked(ActivityStackSupervisor.java:1709)
at com.android.server.am.ActivityStack.resumeTopActivityInnerLocked(ActivityStack.java:3043)
at com.android.server.am.ActivityStack.resumeTopActivityUncheckedLocked(ActivityStack.java:2488)
at com.android.server.am.ActivityStackSupervisor.resumeFocusedStackTopActivityLocked(ActivityStackSupervisor.java:2234)
at com.android.server.am.ActivityStack.completePauseLocked(ActivityStack.java:1745)
at com.android.server.am.ActivityStack.activityPausedLocked(ActivityStack.java:1669)
at com.android.server.am.ActivityManagerService.activityPaused(ActivityManagerService.java:9657)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:224)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3820)
at android.os.Binder.execTransact(Binder.java:752)
p.s. It's not enough for me to get the code in Activity B, I need the code back in Activity A.
Can you please tell me what am I doing wrong here?
Thank you!
just put new intent() instead of
new Intent(getApplicationContext(),A.class)
in your child activity
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.
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();
}
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 have an activity that Call another activity for filling an address and second activity should send back the address for the first activity and show it in a textview in first activity
i used these codes
but i dont know why it s not working
first activity:
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivity(in);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RESULT_OK){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(RESULT_OK, in);
finish();
thanks in advance
You need to start the second activity with startActivityForResult:
http://developer.android.com/reference/android/app/Activity.html#startActivityForResult%28android.content.Intent,%20int%29
In onActivityResult, you should also check the resultCode is RESULT_OK, rather than the requestCode.
I believe you should be using startActivityForResult() instead of startActivity().
first activity
Intent in = new Intent(getApplicationContext(),ShippingActivity.class);
startActivityForResult(in, 0);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 0){
if(resultCode ==1){
tvDeliverTo.setText(data.getStringExtra("DeliveryAdressKEY"));
}
}
}
and second activity
Intent in = new Intent();
in.putExtra("DeliveryAdressKEY", tvAdress.getText().toString());
setResult(1, in);
finish();
and now is working