按下硬键返回/返回按钮时,相机和图库崩溃

时间:2013-11-22 13:35:14

标签: android android-intent android-camera android-keypad

按下硬键返回按钮时相机崩溃。enter image description here这会影响相册中的相册和拍照。我尝试通过检查intent action()是否为空来尝试处理此问题。但它仍然崩溃。非常感谢任何建议。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==0){
        if(data.getAction() != null){
        Bitmap theImage = (Bitmap)data.getExtras().get("data");
        if(theImage !=null){
            iv.setImageBitmap(theImage);
            }
        }

    }
    else if (requestCode == 1) {

        if(data.getAction() != null){
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor =getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

       if(picturePath !=null){
        iv.setImageBitmap(BitmapFactory.decodeFile(picturePath));
       }
     }
    }
}

*** ****更新

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
            //getInfo is the Bundle
    if(data !=null || getInfo !=null){
        if(requestCode==0){
            if(data.getAction() != null){
            Bitmap theImage = (Bitmap)data.getExtras().get("data");
            if(theImage !=null || getInfo !=null || data!=null){
                iv.setImageBitmap(theImage);
                }
            }

        }

1 个答案:

答案 0 :(得分:2)

null内加入onActivityResult()值检查。

在使用之前检查Intent data是否为空以及是否为hasExtra()

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(data!=null){
    // Your code
  }
}