-3

I need to capture an image from the camera and I need to do it from inside a Fragment. My problem is that onActivityResult is never called. Below is my code:

private void dispatchTakePictureIntent(){
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
  }



@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
  if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
     Bundle extras = data.getExtras();
     Bitmap imageBitmap = (Bitmap) extras.get("data");
     im.setImageBitmap(imageBitmap);
  }
  Log.e(TAG,"hello");
  super.onActivityResult(requestCode, resultCode, data);
}

I have looked up several threads here on this forum but none of them are working for me.

Helgegren
  • 137
  • 2
  • 6
  • Possible duplicate of [onActivityResult For Fragment](http://stackoverflow.com/questions/20038880/onactivityresult-for-fragment) – Pankaj May 04 '16 at 09:57

4 Answers4

1

you have to also pass super.onActivityResult(requestCode, resultCode, data); in onActivityResult.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
     Bundle extras = data.getExtras();
     Bitmap imageBitmap = (Bitmap) extras.get("data");
     im.setImageBitmap(imageBitmap);
  }
  Log.e(TAG,"hello");
  super.onActivityResult(requestCode, resultCode, data);
}
Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
1

I tried this way and this worked for me

public class CameraPic extends Fragment {

    private static final int CAPTURE_PICCODE = 989;
    Button button;
    ImageView imageView;

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

        final View rootView = inflater.inflate(R.layout.cameralayout,
                container, false);

        button = (Button) rootView.findViewById(R.id.button1);
        imageView = (ImageView) rootView.findViewById(R.id.imageview1);

        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(intent,
                        CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

            }
        });

        return rootView;

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == Activity.RESULT_OK) {

                Bitmap bmp = (Bitmap) data.getExtras().get("data");
                ByteArrayOutputStream stream = new ByteArrayOutputStream();

                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();



                Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
                        byteArray.length);

                imageView.setImageBitmap(bitmap);

            }
        }        
    }    
}
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
1

Check if you aren't overwriting onActivityResult in your Activity. If so you should call super.onActivityResult from this method. Follow this question to find out the details: onActivityResult is not being called in Fragment

Community
  • 1
  • 1
amukhachov
  • 5,822
  • 1
  • 41
  • 60
0

You can do something like below to call it in fragment First put the code in your onActivityResult() method of activity from which you have created fragment as follows:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Fragment fragment = getSupportFragmentManager().findFragmentByTag("Your Fragment Tag");
        fragment.onActivityResult(requestCode, resultCode, data);
    }

Now in your fragment just put onActivityResult() method and it would be called

Pankaj
  • 7,908
  • 6
  • 42
  • 65