Browsing for files stopped working in WebView

294 views Asked by At

I am trying to make an android app to access a simple Sinatra Website I have created. The Website allows the user to upload photos. Just using WebView the following way allows me to browse my phone for files.

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://10.0.2.2:4567");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

As I tried to make the app better I found this piece of code that makes the app look(and supposedly work) better.

public class MainActivity extends Activity {


    protected WebView myWebView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        myWebView= (WebView) findViewById(R.id.webview);
        myWebView.loadUrl("http://10.0.2.2:4567");
        myWebView.setWebViewClient(new NewWebViewClient());

    }

    private class NewWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView webview,String url)
        {
            webview.loadUrl(url);
            return true;

        }
    }

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
        {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

The problem is now when I tap the browse button in my web page the browsing menu doesn't appear in my screen.Actually nothing happens. I guess NewWebViewClient creates this problem but how is it possible? Is there a way to enable browsing for files again? Do I have to add it manually as I read that it was necessary in earlier android versions?

I am targeting API 18 but the same problem appears for API 17 too.

---------------------------------EDIT-------------------------------

Still having the same problem. openFileChooser gave solution for older APIs but doesn't work for API 18

    myWebView.setWebChromeClient(new WebChromeClient() 

  {   
        public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

  mUploadMessage = uploadMsg;  
  Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
  i.addCategory(Intent.CATEGORY_OPENABLE);  
  i.setType("image/*");  
  Intent chooserIntent = Intent.createChooser(i,"Image Chooser");

  MainActivity16.this.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);  

         }  
  });  

  setContentView(myWebView);  
 }
1

There are 1 answers

0
magmike On BEST ANSWER

https://stackoverflow.com/a/15454070/2394252

Well it has to do with the API after all. Vorrtex gives the solution in the above link. In my case this is the final code.

    public class MainActivity extends Activity {

        WebView myWebView ;
        private ValueCallback<Uri> mUploadMessage;
        private final static int FILECHOOSER_RESULTCODE = 1;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            myWebView = new WebView(this);
            myWebView.setWebViewClient(new WebViewClient());
            myWebView.setWebChromeClient(new WebChromeClient() {
                //The undocumented magic method override  
                //Eclipse will swear at you if you try to put @Override here  
                public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }

                // For Android > 3.x
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }

                // For Android > 4.1
                public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                    MainActivity.this.showAttachmentDialog(uploadMsg);
                }
            });

            this.setContentView(myWebView);

            myWebView.loadUrl("http://10.0.2.2:4567");

        }

        private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
            this.mUploadMessage = uploadMsg;

            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("*/*");

            this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
            if (requestCode == FILECHOOSER_RESULTCODE) {
                if (null == this.mUploadMessage) {
                    return;
                }
                Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
                this.mUploadMessage.onReceiveValue(result);
                this.mUploadMessage = null;
            }
        }

        @Override 
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
            if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack())
            {
                myWebView.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
}

Unfortunately there is no solution for kitkat at the moment since openFileChooser is not available