Application for capturing images by pressing the return button

public class MainActivity extends Activity {
private static final int CAMERA_PIC_REQUEST = 2500;
Button Report_help;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Report_help=(Button)findViewById(R.id.report_help);
    Report_help.setOnClickListener(new OnClickListener()
    {
        public void onClick(View v) {
             Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
             startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    });

}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
          Bitmap image = (Bitmap) data.getExtras().get("data");
          ImageView imageview = (ImageView) findViewById(R.id.display_image);
          imageview.setImageBitmap(image);
    }
}

}

This application captures the image and displays it in the image. But the problem is that after I captured the image and clicked the back button, the application turned off. I do not know why this is so? Please, help.

+2
source share
2 answers

I think when you press the button

Bitmap image = (Bitmap) data.getExtras().get("data");

in onActivityResult cause error Null pointer error , please understand this.

+1
source

Use the code below to verify this case.

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri selectedImageUri = null;
    String filePath = null;
    switch (requestCode) {                
            case PICK_Camera_IMAGE:
                 if (resultCode == RESULT_OK) {
                    //use imageUri here to access the image                

                } else if (resultCode == RESULT_CANCELED) {
                    Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
                }
                 break;
        }

Hope this helps you.

0
source

All Articles