2013-05-16 89 views
-1

我有一個應用程序,在我正在拍照(等)的活動。nullpointerexception當不拍照

現在,當我按下按鈕把它打開camera.If我會按後退按鈕或取消按鈕(未拍照)的照片,它崩潰並給出

空指針

未能提供結果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); 
      } 

至於我記得,上述用於workd用於此目的。 我不知道還有什麼要做。

+0

你可以發佈堆棧跟蹤。 – Raghunandan

+0

你不是在測試resultCode來知道用戶是否按下了。 – njzk2

回答

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 
     } 
    } 
+0

@George請不要評論哪個與答案無關。 –

+0

:好的,對於RESULT_CANCELED我應該把它留空嗎? – George

+0

@George這取決於你的要求,如果你不想做任何事情,當用戶按回來鍵然後不寫任何東西。 –

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){ 
     } 
    } 
}