Ibnu Habibie
Ibnu Habibie

Reputation: 721

startActivityforResult from fragment - get Image from gallery

I've an activity register that have fragment container as viewpager using fragmentpageradapter. this kinda like a wizzard form with next and prev button .

In second fragment inside pager i try to get image from gallery with this code :

private void getAvaFromGallery() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_PICK);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), RESULT_LOAD_IMAGE);
}
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode,data);
        if(resultCode == getActivity().RESULT_OK) {
            if (requestCode == RESULT_LOAD_IMAGE) {
                CompressBitmap cb = new CompressBitmap(getActivity());
                selectedImageUri = cb.handleImageUri(data.getData());
                String filepath = cb.getFilename();
                bitmap = cb.compressImage(cb.getRealPathFromURI(getActivity(), selectedImageUri), filepath);
                photo.setImageBitmap(bitmap);
                Log.d("FilePath", filepath);
            }
        }
    }

after getting data , i try to compress it into png and put it into imageview. but i think onactivityresult is not called in this case.

i've try with only fragment inside activity and this is work . but in my case fragment is inside fragment with pager adapter.

i hope someone can help me ..

Upvotes: 1

Views: 2679

Answers (1)

user4571931
user4571931

Reputation:

I was also facing a similar kind of issue. Below is the solution which worked for me. The code was for Draweractivity, but I was using a FragmentPagerAdapter in DrawerActivity.

  • First of all if you are calling the startActivityForResult() then don’t invoke that method directly. Instead call the getParentFragment().startActivityForResult() in the Child fragment.
  • Implement the onActivityResult() normally without any modifications in the Child fragment.
  • Now in the Main activity which is the parent for both parent and child fragments (in my case the Drawer activity) implement the below code in onActivityResult() which traverses the activity result to all it’s child fragments.

    for (Fragment fragment : getSupportFragmentManager().getFragments())
        {
            if (fragment != null)
            {
                fragment.onActivityResult(p_requestCode, p_resultCode, p_data);
            }
        }
    
  • In the Parent fragment’s onactivityresult(), we again need to implement the code to call the onActivityResult() for it’s child fragments.

List<Fragment> fragments = getChildFragmentManager().getFragments();
            if (fragments != null)
        {
            for (Fragment fragment : fragments)
            {
                Log.e("SupportFragment", fragment.getClass().getSimpleName());
                if(fragment instanceof ContactUsFragment)
                {
                    fragment.onActivityResult(p_requestCode, p_resultCode, p_data);
                }
            }
        }

Note: The code and the snippets is taken from the solution given here

Hope it helps...

Upvotes: 2

Related Questions