Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get image from gallery to set in imageview in fragment? [duplicate]

How to get image from gallary to set in Imageview in fragment ? onActivityResult not called in fragment..this the code i wrote in fragment

img_user.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);


        }
    });


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


        if (data != null) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContext().getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);

            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            img_user.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            cursor.close`enter code here`();
        } else {
            Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT)
                    .show();
        }

    }
like image 757
Niral Bhavsar Avatar asked Dec 08 '15 12:12

Niral Bhavsar


3 Answers

You can do this in this way....in your main activity

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

and inside your fragment

 Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                // ******** code for crop image
                i.putExtra("crop", "true");
                i.putExtra("aspectX", 100);
                i.putExtra("aspectY", 100);
                i.putExtra("outputX", 256);
                i.putExtra("outputY", 356);

                try {

                    i.putExtra("return-data", true);
                    startActivityForResult(
                            Intent.createChooser(i, "Select Picture"), 0);
                }catch (ActivityNotFoundException ex){
                    ex.printStackTrace();
                }

and

@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("data");
                img_user.setImageBitmap(bitmap);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

Works perfect for me

like image 92
H Raval Avatar answered Nov 14 '22 03:11

H Raval


I was also facing the same problem. Actually callback for startActivityForResult() is coming back to parent activity so you need to make explicit call from fragment to onActivityResult() function as follows.

In Parent Activity class, override the onActivityResult() method and even override the same in Fragment Class and call as the following code.

In Parent Class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Fragment fragment = getFragmentManager().findFragmentById(R.id.container);
    fragment.onActivityResult(requestCode, resultCode, data);
}

In Child Class: That Extends fragment perform your logic for image setting on imageview.

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



        if (data != null) {
            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);
                image.setImageBitmap(BitmapFactory.decodeFile(picturePath));

                cursor.close();
            } else {
                Toast.makeText(getActivity(), "Try Again!!", Toast.LENGTH_SHORT).show();
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
like image 22
Durgesh Shankar Avatar answered Nov 14 '22 05:11

Durgesh Shankar


Please find the following code, it will work. Add this in your fragment

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

//Your stuff

}
like image 1
Nigam Patro Avatar answered Nov 14 '22 03:11

Nigam Patro