0

I have a native android application to display a web site. I'm a newbie. I used a webview object to display the site, everything works perfect except the file upload. I tried anything i saw on the internet but i couldn't find a way out of this problem. When i tap the file upload, nothing happens.No error, no other actions... I'm just getting a warning like "openFileChooser is never used locally". I read articles about this warning but i'm really stuck. I'm testing it on a Galaxy S3 Icecream sandwich. Any help will be appreciated.

WebView webView;
private ValueCallback<Uri> mUploadMessage;  
private final static int FILECHOOSER_RESULTCODE=1; 
@Override  
 protected void onActivityResult(int requestCode, int resultCode,  
                                    Intent intent) {  
  if(requestCode==FILECHOOSER_RESULTCODE)  
  {  
   if (null == mUploadMessage) return;  
            Uri result = intent == null || resultCode != RESULT_OK ? null  
                    : intent.getData();  
            mUploadMessage.onReceiveValue(result);  
            mUploadMessage = null;  

  }  
 }  

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form_content_view);
    // Show the Up button in the action bar.
    setupActionBar();

    Intent intent = getIntent();
    final String message = intent.getStringExtra(ListOfForms.EXTRA_MESSAGE);
    WebViewClient wvc=new WebViewClient() {
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
             if (errorCode == 401) {             
                 webView.setHttpAuthUsernamePassword("http://project.mysite.com","mydomain","myuser","mypass");
             }
           }

        public void onReceivedHttpAuthRequest(WebView view,
        HttpAuthHandler handler, String host, String realm)        {
      handler.proceed("myuser", "mypass"); 
        }
    };

    WebChromeClient wcc = new WebChromeClient() {


         public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
         }

         public void openFileChooser(ValueCallback<Uri> uploadMsg) {  

          mUploadMessage = uploadMsg;  
          Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
          i.addCategory(Intent.CATEGORY_OPENABLE);  
          i.setType("image/*");  
          FormContentView.this.startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);  

         } 
    };

    webView = (WebView) findViewById(R.id.wvFormContent);

    webView.setWebChromeClient(wcc);

    webView.setWebViewClient(wvc);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setGeolocationEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setAllowFileAccess(true);
    webView.getSettings().setAllowContentAccess(true);
    webView.getSettings().setAppCacheEnabled(true);
    webView.getSettings().setDatabaseEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.loadUrl("http://project.mysite.com");

    String myVariable = "dummy";
    webView.loadUrl("javascript:document.getElementById('txtMyVariable').text = '"+myVariable+"';");

}

private void setupActionBar() {

    getActionBar().setDisplayHomeAsUpEnabled(true);

}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:

        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Emre Çınar
  • 30
  • 2
  • 9

2 Answers2

0

I found the solution, i created different openFileChooser functions for different android versions. My version is 4.1.2 and i declared my handler function like :

public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture)

If i declare this way file upload call hits the function. So the example code i wrote at the beginning post is not valid for Android 4.1.2.

Emre Çınar
  • 30
  • 2
  • 9
0

Adding multiple openFileChooser, showFileChooser and onShowFileChooser methods works for most Android versions, but fails for Android 4.4 KitKat (which represents 29% of current Android devices) that has a WebView bug that broke file inputs.

My workaround to make it work on every Android version is to communicate between the app and the website using Javascript, and detect when the user wants to upload a file, and upload it from the app (outside the webview), and then update the webview when the file finishes loading, to tell the website where the file is located in the server.

Please check my answer here where I explain my solution: https://stackoverflow.com/a/43443981/2566593

Community
  • 1
  • 1