-1

I want to display a photo from device camera to an Image View I did it before with activity but when I try it on fragment I found that problem:

 java.lang.RuntimeException: Failure delivering result
 ResultInfo{who=null, request=1888, result=-1, data=Intent {
 act=inline-data (has extras) }} to activity
 {com.example.uhf/com.example.uhf.activity.UHFMainActivity}:
 java.lang.NullPointerException: Attempt to invoke virtual method 'void
 android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a
 null object reference
         at android.app.ActivityThread.deliverResults(ActivityThread.java:4295)

my code is too simple:

ImageView person_photo;
private static final int CAMERA_REQUEST = 1888;




 Button open_camera = (Button)getView().findViewById(R.id.take_photo);
        open_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i,CAMERA_REQUEST);

            }
        });




@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode != RESULT_CANCELED){
            if (requestCode == CAMERA_REQUEST) {
                Bitmap photo = (Bitmap) data.getExtras().get("data");
                person_photo.setImageBitmap(photo);
            }
        }

    }
SkypeDogg
  • 1,000
  • 2
  • 13
  • 28
omar bouhajja
  • 105
  • 1
  • 1
  • 11

1 Answers1

2

get the reference of the person_image view.

person_photo= (ImageView)findViewById(R.id.person_photo_id);

I hope works!

Hassan Salah
  • 157
  • 2
  • 7