Search code examples
androidandroid-fragmentsfragmentandroid-fragmentactivityonactivityresult

In Fragment Doesn't gave response for onActivityResult when I clicked the Image from gallary in Android


I have a problem when I click button Image from gallary to set in imageview,but OnActivityResult doesn't response at any cost.

I don't know how to override this in Activity there is nothing response over there.

How to solve this problem.Thanks in Advance

Code:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_edit_photo, null);

        img = (ImageView)view.findViewById(R.id.img);
        choosePhoto = (Button) view.findViewById(R.id.choose_photo_button);
        Button clickPhoto = (Button) view.findViewById(R.id.click_photo_button);
        toolbar = (Toolbar)view.findViewById(R.id.toolbar);


        choosePhoto.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);



                //startActivityForResult(i, RESULT_LOAD_IMAGE);

            }
        });


        clickPhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                // start the image capture Intent
                startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);

            }
        });



      /*  toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               onBackPressed();

            }
        });*/




        return view;



    }

OnactivtiyResult

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor =getActivity().getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


        img.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }


}

Solution

  • Override this method on your main activity

     @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    
        YourFragment yourFragment= (YourFragment ) getSupportFragmentManager().findFragmentByTag("yourTag");
    // or YourFragment yourFragment = (YourFrament) getSupportFragmentManager().findFragmentById(R.id.fragment_layout_id);
        demoFragment.onActivityResult(requestCode, resultCode, data);
    }
    

    after your fragment activity result call