5

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:

E/AndroidRuntime( 3891): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity {net.asplode.tr/net.asplode.tr.PostImage}: java.lang.NullPointerException
E/AndroidRuntime( 3891):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
E/AndroidRuntime( 3891):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
E/AndroidRuntime( 3891):    at android.app.ActivityThread.access$2800(ActivityThread.java:125)
E/AndroidRuntime( 3891):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
E/AndroidRuntime( 3891):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 3891):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 3891):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 3891):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 3891):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 3891):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 3891):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 3891):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 3891): Caused by: java.lang.NullPointerException
E/AndroidRuntime( 3891):    at net.asplode.tr.PostImage.onActivityResult(PostImage.java:92)
E/AndroidRuntime( 3891):    at android.app.Activity.dispatchActivityResult(Activity.java:3890)
E/AndroidRuntime( 3891):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
E/AndroidRuntime( 3891):    ... 11 more
W/ActivityManager(   85):   Force finishing activity net.asplode.tr/.PostImage

Code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.mnuCamera) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        ContentValues values = new ContentValues();
        values.put(Media.TITLE, "image");
        Uri tempPhotoUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempPhotoUri);
        startActivityForResult(cameraIntent, FROM_CAMERA);
        return true;
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) {
        return;
    }
    Uri imageUri = data.getData();
    Log.i("imageUri: ", imageUri.toString());
}
Niall Sheridan
  • 1,536
  • 2
  • 10
  • 6

4 Answers4

6

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.

Niall Sheridan
  • 1,536
  • 2
  • 10
  • 6
  • In onActivityResult() if the intent is null, then how to get the imagepath @nsheridan – Abhi Jan 02 '12 at 14:04
  • 3
    @nsheridan - Sorry to bother on an old question, I'm having the same issue but really getting crazy cause I can't get it to work. Could you please post some code to show your solution?THks in advance... – Matteo Sep 26 '12 at 09:46
3

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

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • You're right, that's not really a question. Here's my question: can anyone help me pinpoint why I'm getting an npe with data=null? I know the intent is null, the stacktrace tells me: ResultInfo{who=null, request=0, result=-1, data=null} I have cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempPhotoUri); in the above code also. I believe this is correct. – Niall Sheridan Jul 18 '10 at 17:12
  • Did you see this other question on StackOverflow? http://stackoverflow.com/questions/1910608/android-action-image-capture-intent – nicholas.hauschild Jul 19 '10 at 05:00
1

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.

Flep
  • 301
  • 3
  • 6
0

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.

Daniel Gomez Rico
  • 15,026
  • 20
  • 92
  • 162