4

I've got a problem with the webview embeded in my app (Android 4.4.4+). On a page there is an "Upload image" button I manage with openFileChooser.

On some devices, when I click on the "Upload image" button in the webview, the camera app is launched and my app is destroyed. After taking the picture my app is recreated, I restore the webview state (saved in onSaveInstanceState) and the onActivityResult() is called. The problem is that the variable mUploadMessage containing the ValueCallBack is null, so I'm not able to invoke the ValueCallBack.onReceiveValue() to give the image URI to the webview.

// openFileChooser for Android 3.0+ to 4.4
// WARNING: the openFileChooser works in version 4.4 only for 4.4.3+
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
    // Update message
    mUploadMessage = uploadMsg;
    fileChooserManagement(FILECHOOSER_RESULTCODE);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    webview.saveState(outState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        // Restore last state
        webview.restoreState(savedInstanceState);
    }
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Do not test the resultCode, because it's mandatory to enter the 'if' and to call the
    // onReceiveValue even with null. Otherwise the openFileChooser will not be called anymore.
    if (requestCode == FILECHOOSER_RESULTCODE) {
        if (this.mUploadMessage == null) {
            Log.i("myApp", "onActivityResult mUploadMessage is null");
            return;
        }
        mUploadMessage.onReceiveValue(processPicture(resultCode, data));
        mUploadMessage = null;
    }
}

There's a similar topic here, but the question "I didn't understand how to save the webview state and restore it. If I restore it in onRestoreInstanceState() the page does not take the picture from the camera.. Would you give me an example?" has no answer.

Thank you for your help guys!

Community
  • 1
  • 1
Guillaume
  • 41
  • 2

0 Answers0