Search code examples
androidimageviewsetbackground

Take image from gallery and set to a CircleImageView


So I'm inside a fragment and I want to take an image from gallery and set to a CircleImageView. I start an intent and correctly choose the image but on the onActivityResult the image is not setted. The default src image remains. This is the code

fragment_layout

<de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/photo"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:src="@drawable/ic_launcher_background"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="15dp"
                />

fragment class

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LayoutInflater lf = getActivity().getLayoutInflater();
        view =  lf.inflate(R.layout.fragment_profile,container,false);
        mContext = getActivity();

        mProfilePhoto = (CircleImageView) view.findViewById(R.id.profile_photo);
        view.findViewById(R.id.profile_photo).setOnClickListener(this);

        return view;

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.profile_photo:
                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                try {
                    i.putExtra("return-data", true);
                    startActivityForResult(
                            Intent.createChooser(i, "Select Picture"), 0);
                }catch (ActivityNotFoundException ex){
                    ex.printStackTrace();
                }
                break;
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==0 && resultCode == Activity.RESULT_OK){
            try {
                Bundle bundle = data.getExtras();
                Bitmap bitmap = bundle.getParcelable("return-data");
                mProfilePhoto.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

and the activity which implements this fragment has its onActivityResult whith just the call at super();

Also, after loading the image, will it be resized automatically to fit the circle image view? if not how should I do? Thanks in advance


Solution

  • It seems like I have experienced this problem. And I am using your library for all my projects.

    if (requestCode != RESULT_CANCELED){
                if (requestCode == GALLERY_REQUEST){
                    Uri path = data.getData();
                    try {
                        photo = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(), path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mProfilePhoto.setImageBitmap(photo);}}