Search code examples
androidcameraonactivityresult

Android onActivityResult doesn't activate for the camera?


I tried to learn how to use the camera with android , and for some reason after the camera is done the photo just doesn't load. I have 2 buttons and an image in the view , 1 for the camera and 1 to load the picture but I want to load the picture without the button.. (Both buttons work fine , onActivityResult just doesn't seem to work)

static final int CAM_REQUEST = 1;
button = (Button) findViewById(R.id.button);
        imageView = (ImageView) findViewById(R.id.imageView);
        Button button2 = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent , CAM_REQUEST);
            }
        });

button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String path = "sdcard/camera_app/cam_image.jpg";
                imageView.setImageDrawable(Drawable.createFromPath(path));

            }
        });

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

    String path = "sdcard/camera_app/cam_image.jpg";
    imageView.setImageDrawable(Drawable.createFromPath(path));
}

as you can see the code of button2 is the same as onActivityResult but the button loads the picture while onActivityResult doesn't..


Solution

  • Call the super in onActivityResult()

    It should look like this:

     super.onActivityResult(requestCode, resultCode, data);