0

I have the application to take the photo and send it to server. The taking photo and server(accept image) run perfectly separately. When I try to use below code after taking photo to send server, it causes to force to close the application. Here is the code:

protected void onActivityResult(int requestCode, int resultCode, Intent data){
   if (resultCode == RESULT_OK){
      Uri imageUri=data.getData();
      List<NameValuePair> params = new ArrayList<NameValuePair>(1);
      params.add(new BasicNameValuePair("image", imageUri.getPath()));
      post("http://myserver-address",params);
   }
}

post method is same as Sending images using Http Post

Log cat says:

java.lang.RuntimeException: Unable to resume activity
{com.john.smith/com.john.smith.Activity}: java.lang.RuntimeException:
Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null}
to activity {com.john.smith/com.john.smith.Activity}:
   java.lang.NullPointerException

I think, there is something wrong for passing parameters to send function. Because, I try disable all the lines in my send function, still face the error. And this error causes to force the application close.

How can I fix that problem ? How to pass the photo to send function other than mine ?

Update:

my camera capture code is same as http://code.google.com/p/crw-cmu/source/browse/luis/PhotoIntentActivity/src/com/example/android/photobyintent/PhotoIntentActivity.java?r=734

I call it like that

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

        switch (requestCode) {
        case ACTION_TAKE_PHOTO_B: {
            if (resultCode == RESULT_OK) {
            handleBigCameraPhoto();
          Uri imageUri=data.getData();
          List<NameValuePair> params = new ArrayList<NameValuePair>(1);
          params.add(new BasicNameValuePair("image", imageUri.getPath()));
          post("http://myserver-address",params);
        }
            }
            break;
        } // ACTION_TAKE_PHOTO_B

    } // switch
}
Community
  • 1
  • 1
John Smith
  • 2,668
  • 7
  • 30
  • 34
  • Check if `imageUri` is null after this line `Uri imageUri=data.getData();`. Also, what's in `handleBigCameraPhoto`? – Mike Park Nov 08 '12 at 17:46
  • I checked Uri imageUri=data.getData(); and it does return null, How to get the image that captured from camera ? – John Smith Nov 08 '12 at 18:00

1 Answers1

0

The log show data=null so Uri imageUri=data.getData(); cause a NPE.

I suggest to add some if( requestCode == ...) { or switch( requestCode ) because the data is not set in all the cases.

To avoid a crash you should check for null before dereferencing data and imageUri. Add some log in the else clauses.

An Internet search provides results:

Community
  • 1
  • 1
Aubin
  • 14,617
  • 9
  • 61
  • 84
  • Yes, you are right, When I try to use imageUri, it returns null. So, how can I get the image that captured from the camera and send it to send function. – John Smith Nov 08 '12 at 17:57
  • I applied your suggestion, the thing is that my imageUri is always null because, data comes null. So, how to fix it ? – John Smith Nov 08 '12 at 19:24
  • I have a question, this code 'params.add(new BasicNameValuePair("image", imageUri.getPath()));' sends the path of the image not the image itself, Right ? – John Smith Nov 08 '12 at 19:45
  • Okay, thanks. I already saw these links. I choose the best one as you highlighted. Now, the question turns into that Where should I call the post function, it seems calling in "onActivityResult" not good. ? – John Smith Nov 08 '12 at 20:19