I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture

前端 未结 4 1007
别跟我提以往
别跟我提以往 2020-11-30 14:24

I have a fairly simple app that launches the camera from a menu. The camera launches fine, but when I hit ok after taking a picture I get a NPE on my nexus one:



        
相关标签:
4条回答
  • 2020-11-30 14:45

    onActivityResult is called when any activity that you have already started ends, so if you start an activity that dont send data there will be a problem.

    0 讨论(0)
  • 2020-11-30 15:02

    This doesn't really seem like a question, more like a factual statement. If you are asking what is null, there are two things that can be null:

    -The Intent 'data'
    -The Uri 'imageUri'

    Did you add the Extra, 'EXTRA_OUTPUT', to the Intent? If not, you will only be able to retrieve a small sized image in the Extra field. And this would seem to perhaps be your NPE, happening on 'imageUri'.

    http://developer.android.com/reference/android/provider/MediaStore.html#ACTION_IMAGE_CAPTURE

    0 讨论(0)
  • 2020-11-30 15:07

    Turns out the stock camera application doesn't send EXTRA_OUTPUT, which is why it's null. However, some camera apps (like the hero) do. Awesome. So the answer is to specify EXTRA_OUTPUT. The nexus one camera app will save the image to that location. Then in onActivityResult() check if the intent is null. If it isn't, use data.getData(), and if it is then use the location specific in EXTRA_OUTPUT via a constant and insert it into the Mediastore. Urgh.

    0 讨论(0)
  • 2020-11-30 15:07

    Based on nsheridan's solution, i just made the fileUri that I added in the intent (intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);) known throughout the class. In the ActivityResult(), I checked whether the intent == null, if so, the fileUri variable is used instead of trying to get it out of the intent.getData().

    Works fine for me now.

    0 讨论(0)
提交回复
热议问题