2
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);
    }
}

}

这个应用程序捕获图像并显示在图像视图中。但问题是在我捕获图像并按下后退按钮后应用程序崩溃。我不知道为什么会这样?请任何人帮助。

4

2 回答 2

1

我想当你按下后退按钮时

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

在 onActivityResult 导致Null 指针异常错误,请抓住这个。

于 2013-05-06T12:53:20.663 回答
0

使用下面的代码来检查这种情况。

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;
        }

希望这可以帮助你。

于 2013-05-06T12:43:29.030 回答