How to download image from web view?

I have a problem with webview embedded in my application (Android 4.4.4+). There is a button “Download Image” on the page that I control with openFileChooser.

On some devices, when I click the “Download Image” button in a web browser, the camera application launches and my application is destroyed. After the image has been restored, I restore the state of the web view (saved to onSaveInstanceState) and called onActivityResult(). The problem is that the variable mUploadMessagecontaining ValueCallBackis null, so I cannot call ValueCallBack.onReceiveValue()to provide 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 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;
    }
}

, " , - . onRestoreInstanceState() . ?" .

!

+4
1

, webview . webview, - chrome Android. , URL. , . , . ,

0

Source: https://habr.com/ru/post/1620726/


All Articles