-1

我有一个应用程序,在活动中我正在拍照(除其他外)。

现在,当我按下按钮拍照时,它会打开相机。如果我按下后退按钮或取消按钮(不拍照),它会崩溃并给出

空指针

传递结果 ResultInfo 失败

在这一行:

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

我用:

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

                      if(requestCode == CAMERA_REQUEST){      


                            Bitmap photo = (Bitmap) data.getExtras().get("data");
                            imageView.setImageBitmap(photo);
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                            photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
                            blobvalue = stream.toByteArray();

                             Bundle extras = new Bundle();
                             Intent k=new Intent(this,MainActivity.class);  
                             extras.putParcelable("Bitmap", photo);
                             k.putExtras(extras);

                      }

                      if (requestCode == RESULT_CANCELED) {    

                      }        


           }

在我的适配器中:

ImageView myImage=(ImageView) convertView.findViewById(R.id.myimage);

            final Bitmap image;
            if(theItems.getImagemyItems() != null)
            {
                byte []temp = theItems.getImagemyItems();
                image = BitmapFactory.decodeByteArray(temp, 0, temp.length);
                myImage.setImageBitmap(image);
            }
            else
            {
                image = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
                myImage.setImageBitmap(image);
            }

据我记得,以上内容曾经为此目的工作。我不知道还能做什么。

4

2 回答 2

1

您刚刚测试了 requestCode 但没有 resultCode 所以我建议您检查 resultCode 用户是否已捕获图像或取消捕获。

尝试:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == CAMERA_REQUEST){  
        if (resultCode == Activity.RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
        }
        else if (resultCode == Activity.RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
于 2013-05-16T09:15:32.800 回答
0

您只需在您的 中进行检查onActivityResult,情况RESULT_OK是用户成功拍照,情况RESULT_CANCELLED是您按下硬件后退按钮并想要返回您的活动。

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
   if(requestCode == CAMERA_REQUEST){      
      if(resultCode == RESULT_OK){
      // your code comes here
      }
      if(resultCode == RESULT_CANCELED){  
      }
   }
}
于 2013-05-16T09:17:51.483 回答