0

I'm trying to make an application based on webview. So far everything works fine except that when I click on an file input nothing happens. I have searched for many solutions on the internet but none has worked for me, even those of this question: File Upload in WebView This is my MainActivity.java code:

package com.myapp.myapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    WebView wv;
    private ProgressBar progressBar;
    @Override
    public void onBackPressed(){
        if(wv.canGoBack()){
            wv.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        wv = (WebView) findViewById(R.id.wv);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusable(true);
        wv.setFocusableInTouchMode(true);
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        wv.getSettings().setDomStorageEnabled(true);
        wv.getSettings().setDatabaseEnabled(true);
        wv.getSettings().setAppCacheEnabled(true);
        wv.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        wv.loadUrl("https://myapp.com");
        wv.setWebViewClient(new WebViewClient());

        progressBar = (ProgressBar) findViewById(R.id.progressbar);

        wv.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int progress)
            {
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                MainActivity.this.setProgress(progress * 1000);

                progressBar.incrementProgressBy(progress);

                if(progress == 100){
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
    }
}
Community
  • 1
  • 1
  • what the error ? can you post your logcat ? – MAS. John Apr 07 '17 at 01:31
  • even this question [File Upload in WebView](http://stackoverflow.com/questions/5907369/file-upload-in-webview) doesn't work ? have you "overrided" all version of openFileChooser, from android 2.3+ to 5.0+ ? And, handle onActivityResult properly ? – jiashie Apr 07 '17 at 01:41
  • openFileChooser is gray and says: Method "openFileChooser(android.webkit.ValueCallback, java.lang.String)" is never used. I think it's the mistake, but I do not know how to fix it. I am new at this. – Leonardo Massaroli Apr 08 '17 at 14:09

1 Answers1

0

you have to override openFileChooser in WebChromeClient

      public void openFileChooser(ValueCallback<Uri> uploadFile) {    
                            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                            i.addCategory(Intent.CATEGORY_OPENABLE);  
                            i.setType("*/*");  
                            startActivityForResult(Intent.createChooser(i, getString(R.string.choose_upload)), FILE_SELECTED);  
       }  

add this method in class WebChormeClient:

wv.setWebChromeClient(new WebChromeClient(){

            public void openFileChooser(ValueCallback<Uri> uploadFile) {    
                            Intent i = new Intent(Intent.ACTION_GET_CONTENT);  
                            i.addCategory(Intent.CATEGORY_OPENABLE);  
                            i.setType("*/*");  
                            startActivityForResult(Intent.createChooser(i, getString(R.string.choose_upload)), FILE_SELECTED);  
            }  


            @Override
            public void onProgressChanged(WebView view, int progress)
            {
                progressBar.setProgress(0);
                progressBar.setVisibility(View.VISIBLE);
                MainActivity.this.setProgress(progress * 1000);

                progressBar.incrementProgressBy(progress);

                if(progress == 100){
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
xiaoyuan
  • 423
  • 4
  • 13