10

I have an webview with file chooser which is works in Samsung Galaxy Tab 2(Android 4.1.1), Lenovo, Nexus Tablets. But the problem is it is not woking in Samsung galaxy Tab 3. Which is the Android 4.4 tablet. I added my setWebChromeClient code here. Can you Please someone help me.

// implement WebChromeClient inner class
        // we will define openFileChooser for select file from camera
        webView.setWebChromeClient(new WebChromeClient() {

            // openFileChooser for Android 3.0+
            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                    String acceptType) {
                /** updated, out of the IF **/
                mUploadMessage = uploadMsg;
                /** updated, out of the IF **/
                Log.e("Reac", "**Here");
                try {
                    File imageStorageDir = new File(base_directory,
                            "profile_pictures");
                    if (!imageStorageDir.exists()) {
                        imageStorageDir.mkdirs();
                    }
                    src_file = new File(imageStorageDir + File.separator
                            + "IMG_" + child_id + ".jpg");
                    mCapturedImageURI = Uri.fromFile(src_file); // save to the
                                                                // private
                                                                // variable

                    final Intent captureIntent = new Intent(
                            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                            mCapturedImageURI);

                    startActivityForResult(captureIntent,
                            FILECHOOSER_RESULTCODE);
                } catch (Exception e) {
                    Toast.makeText(getBaseContext(), "Camera Exception:" + e,
                            Toast.LENGTH_LONG).show();
                }
            }



            // openFileChooser for Android < 3.0
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooser(uploadMsg, "");
            }

            // openFileChooser for other Android versions
            public void openFileChooser(ValueCallback<Uri> uploadMsg,
                    String acceptType, String capture) {
                openFileChooser(uploadMsg, acceptType);
            }

            /** Added code to clarify chooser. **/

            // The webPage has 2 filechoosers and will send a console message
            // informing what action to perform, taking a photo or updating the
            // file
            public boolean onConsoleMessage(ConsoleMessage cm) {
                onConsoleMessage(cm.message(), cm.lineNumber(), cm.sourceId());
                return true;
            }

            public void onConsoleMessage(String message, int lineNumber,
                    String sourceID) {
                // Log.d("androidruntime", "Per cÔøΩnsola: " + message);
            }
            /** Added code to clarify chooser. **/

        });

I added only piece of code. Please ask me if any details required.

Amsheer
  • 7,046
  • 8
  • 47
  • 81

1 Answers1

5

It seems there is a known bug in Android 4.4 that make <input type="file"> not work inside a WebView.

Unfortunately openFileChooser is not a public API and there is not a public support for that. In Android Lollipop has been introduced the API onShowFileChooser for this purpose.

My suggest is to workaround the problem using the JavaScript native WebView interface and define a custom openFileChooser API handled by the native part of your App.

Pollizzio
  • 1,023
  • 8
  • 11
  • If i try to use javascript native webview interface then how to get input for this method public void openFileChooser(ValueCallback uploadMsg, String acceptType) {? – Amsheer Jul 21 '15 at 10:06
  • The idea is to completely workaround the standard file chooser behavior by: 1: Having a button on web part that call the injected javascript API "openFile". 2: Handle the openFile on native part by launching an intent to pick up a file. 3: Handle on native parte the onActivityResult resolving the path of the file choose. 4: Send the path to the Web part by a custom JavaScript API (e.g. webView.evaluateJavaScript("selectedFile("path") ", null); 5: Handle on Web part the logic with the file selected. You may need to change some WebView settings as WebSettings.setAllowUniversalAccessFromFileURLs – Pollizzio Jul 21 '15 at 15:28
  • Hi do u got any solution. i have same problem with samsung tab 3.please post your answer if you got solution – benarjee bojja Aug 27 '16 at 07:40