-1

I have one fragment and one activity.. In my layout i have one image view and one button. I wrote some coding for pick image from galley but i cant place that image on imageView.. I dont Konw how to do. Please any one fix this issue.

My code here:

ProfileFragment.java

public class ProfileFragment extends Fragment {

    private ImageView imageView;
    private static final int SELECT_PHOTO = 1;

    Button browseProfilePic;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setHasOptionsMenu(true);//For option menu

        View view = inflater.inflate(R.layout.fragment_layout_profilepic, container,
                false);

        imageView = (ImageView)view.findViewById(R.id.profile_image);
        browseProfilePic = (Button) view.findViewById(R.id.btn_pick);

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

                Intent intent = new Intent();
                // Show only images, no videos or anything else
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                // Always show the chooser (if there are multiple options available)
               startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PHOTO);

            }
        });

        return view;
    }

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

        if (requestCode == SELECT_PHOTO && resultCode == getActivity().RESULT_OK && data != null && data.getData() != null) {

            Uri uri = data.getData();

            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

                imageView.setImageBitmap(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu,MenuInflater inflater ) {
        super.onCreateOptionsMenu(menu, inflater);
        // Inflate the menu; this adds items to the action bar if it is present.
        inflater.inflate(R.menu.menu_profile_pic, menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()){
            case R.id.action_done:
                Toast.makeText(getActivity().getApplicationContext(), "Sa", Toast.LENGTH_LONG).show();

        }
        return super.onOptionsItemSelected(item);

    }

    }

MainActivity.java:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {

    }
Shayan Ghosh
  • 882
  • 6
  • 14
Razul
  • 99
  • 1
  • 12

1 Answers1

0

If the Activity hosting the fragment overrides onActivityResult like your MainActivity does then just make sure it calls super.onActivityResult (there's no need to completely remove the override), that should give the fragment a chance to handle the result see, otherwise it assumes the MainActivity.java handled the result and won't call the fragment onActivityResult

If even so the image won't show first confirm that the onActivityResult is not being called by placing a Toast on the beginning of the fragment onActivityResult place the result here and we will work from there

Also dont: getActivity().RESULT_OK use Activity.RESULT_OK

Edit: just tested it out with your code and calling super.onActivityResult as expected it works perfect, and won't work without it

Community
  • 1
  • 1
forcewill
  • 1,637
  • 2
  • 16
  • 31