Android onActivityResult intent data null or empty extra data


When we’re using Google Camera tutorial on Samsung Galaxy S3, we got empty results for passing extra data in onActivityResult.
In another word, we got NullPointerException when trying to get data out of intent in onActivityResult

Why? Apparently, there is a bug in putting MediaStore.EXTRA_OUTPUT into Photo Intent:

1
2
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
        Uri.fromFile(photo));

If we disabled this line, then we can pass whatever extra data we want to onActivityResult.

Wait, that’s not the really problem here. Apparently, once we switch the camera off, then Activity will automatically reset and that’s mean the extra variable we passed into takePhotoIntent is gone!

So what the solution?
Remember with lifecycle callbacks? Yes, basically instead of putting extra data into Intent and got nothing in onActivityResult, we can put it into SaveInstanceState and take them back through onRestoreInstanceState.

Confuse? Here is the implementation, I will use currentPhotoPath as example extra data that I was put into Intent extra data.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
private static final String IMAGE_PATH = "path";
String currentPhotoPath = "";

/**
 * Intent to handle capture image from camera and save into a file
 * @param actionCode
 */
 private void dispatchTakePhotoIntent(int actionCode) {
        Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       
        switch(actionCode) {
        case CAMERA_REQUEST:
            File photo = null;
           
            try {
                photo = setupPhotoFile();
                takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        Uri.fromFile(photo));
                currentPhotoPath = photo.getAbsolutePath();
                setResult(Activity.RESULT_OK, takePhotoIntent);
                takePhotoIntent.putExtra("return-data", true);
        } catch (IOException e) {
            e.printStackTrace();
            photo = null;
            currentPhotoPath = null;
        }
        break;
   
    default:
        break;
    }
   
    startActivityForResult(takePhotoIntent, actionCode);
}

/**
 * Process photos that already saved by the users after taken from Camera
 */
@Override
protected void onActivityResult(int requestCode, int resultCode,
        Intent data) {
    switch(requestCode) {
    case CAMERA_REQUEST: {
        if(resultCode == Activity.RESULT_OK) {
            Log.v("ANDROIDDEBUG", currentPhotoPath);
        }
        break;
    }
    }
}

// Some lifecycle callbacks so that the image can survive orientation change
@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString(IMAGE_PATH, currentPhotoPath);
   
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
   
    currentPhotoPath = savedInstanceState.getString(IMAGE_PATH);
}

Easy right? 🙂


One response to “Android onActivityResult intent data null or empty extra data”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.