Search code examples
androidwebviewclientwebchromeclient

Upload photo from gallery or take from camera in Webview


My application is webbased and need to upload photos, website have a file input button, i made it work with this

wv = new WebView(this);
wv.setWebViewClient(new WebViewClient());  
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setAllowFileAccess(true);

wv.setWebChromeClient(new WebChromeClient()  
{    
         public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
               mUploadMessage = uploadMsg;  
               Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
               i.addCategory(Intent.CATEGORY_OPENABLE);  
               i.setType("image/*");  
               MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );

}

but it shows just gallery to pick photos, i need to take from camera at the same time.

i tried this solution Upload camera photo and filechooser from webview INPUT field but its only opening camera, not uploading taken photo


Solution

  • In your example

    wv.setWebViewClient(new WebViewClient() {      
       public boolean shouldOverrideUrlLoading(WebView v, String url) { 
         if (url.startsWith("testzapp:")) {
           //do whatever action you had intended
         }
       }
     }