image capture app crashes on pressing back button

血红的双手。 提交于 2019-12-22 17:32:08

问题


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 app captures the image and displays in the imageview.But the problem is after I capture the image and press the back button app crashes.I don't know why is this so? Please anyone help.


回答1:


I think when you press back button

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

in onActivityResult cause the Null pointer exception error, please catch this one.




回答2:


Use the below code to check that 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.



来源:https://stackoverflow.com/questions/16397847/image-capture-app-crashes-on-pressing-back-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!