拍照:java.lang.RuntimeException:传递结果ResultInfo失败

时间:2014-09-29 15:04:10

标签: android fragment capture

我尝试在我的应用中拍照,但当我拍照并返回我的应用时,我的应用程序崩溃了!这段代码是碎片。

 addImage.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View view) {


            // create Intent to take a picture and return control to the calling application
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


            // start the image capture Intent
            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

       }
});

onActivityResult:

 public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent){
                super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

                if(resultCode == RESULT_OK)
                {
                    Uri selectedImage = imageReturnedIntent.getData();
                    Bitmap mBitmap = null;
                    try
                    {
                        mBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                        if(mBitmap!=null) {
                            Log.v("Bitmap", mBitmap + "");
                        }
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                    }
                }
            }

这是错误:

09-29 14:49:20.594    2857-2857/com.ex.com E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.ex.com, PID: 2857
    java.lang.RuntimeException: Unable to resume activity {com.ex.com/com.ex.com.activities.MainActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196708, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.ex.com/com.ex.com.activities.MainActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)

1 个答案:

答案 0 :(得分:0)

在OnActivityResult()

       if(imageReturnedIntent!=null)
          Bitmap picture = (Bitmap) imageReturnedIntent.getExtras().get("data");