HTML input type 'file' is not working on webview in android. Is there any way to upload file using webview android?

前端 未结 4 776
伪装坚强ぢ
伪装坚强ぢ 2020-12-14 03:25

this is my browsing code of html.

This is my android code which load html file for browse file and upload on server.

        runOnUiThread(new R         


        
4条回答
  •  醉话见心
    2020-12-14 04:02

    This code is working for me.....

    Here, no need to ask storage permissions to the user....

    package app.qarya.in.upsc4u.questions.answers;
    import android.annotation.SuppressLint;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.ProgressDialog;
    import android.content.ActivityNotFoundException;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.net.Uri;
    import android.os.Build;
    import android.os.Bundle;
    import android.os.Handler;
    import android.support.annotation.NonNull;
    import android.support.annotation.Nullable;
    import android.support.annotation.RequiresApi;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.view.KeyEvent;
    import android.view.View;
    import android.webkit.ValueCallback;
    import android.webkit.WebChromeClient;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    import android.widget.FrameLayout;
    import android.widget.ProgressBar;
    import android.widget.Toast;
    
    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    import com.google.android.gms.ads.InterstitialAd;
    import com.google.android.gms.ads.MobileAds;
    import com.google.android.gms.tasks.OnCompleteListener;
    import com.google.android.gms.tasks.Task;
    
    
    public class WebPageMain extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
    
        String webAddress = "https://qarya.in/";
        public static final int REQUEST_SELECT_FILE = 100;
        WebView webView;
        ProgressBar progressBar;
        FrameLayout frameLayout;
        SwipeRefreshLayout swipeRefreshLayout;
        ProgressDialog pd;
        static int count=0;
        AdView adView;
        AdRequest adRequest;
        InterstitialAd interstitialAd;
        private ValueCallback uploadMessage;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.web_page);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    
             MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
    
    
    
            pd = new ProgressDialog(this);
            pd.setMessage("Loading please wait...");
            pd.show();
    
            swipeRefreshLayout = findViewById(R.id.srl);
            swipeRefreshLayout.setOnRefreshListener(this);
    
            frameLayout = findViewById(R.id.framLayout);
            progressBar = findViewById(R.id.progressBar);
            assert webView != null;
            progressBar.setMax(100);
            webView = findViewById(R.id.webView);
            webView.setWebViewClient(new Callback());
            webView.getSettings().setJavaScriptEnabled(true);
            webView.setVerticalFadingEdgeEnabled(false);
            webView.loadUrl(webAddress);
            progressBar.setProgress(0);
            adView = findViewById(R.id.adView);
            adRequest = new AdRequest.Builder().build();
            //adView.loadAd(adRequest);
    
            interstitialAd = new InterstitialAd(this);
            interstitialAd.setAdUnitId(getResources().getString(R.string.inter));
            //interstitialAd.loadAd(adRequest);
            //showInterAd();
    
    
            webView.setWebChromeClient(new WebChromeClient() {
    
                public void onProgressChanged(WebView webView,int progress)
                {
                    frameLayout.setVisibility(View.VISIBLE);
                    progressBar.setProgress(progress);
                    //setTitle("Loading...");
    
                    if(progress==100)
                    {
                        frameLayout.setVisibility(View.GONE);
                        pd.dismiss();
                        //setTitle(webView.getTitle());
                    }
                    else if(progress==70)
                    {
                        pd.dismiss();
                        count+=1;
                        if(count%5==0) {
                            interstitialAd.loadAd(adRequest);
                            showInterAd();
                        }
                        adView.loadAd(adRequest);
                    }
    
                    //Log.d("progress",progress+"");
                    super.onProgressChanged(webView,progress);
                }
    
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                public boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, FileChooserParams fileChooserParams) {
                    // make sure there is no existing message
                    if (uploadMessage != null) {
                        uploadMessage.onReceiveValue(null);
                        uploadMessage = null;
                    }
    
                    uploadMessage = filePathCallback;
    
                    Intent intent = fileChooserParams.createIntent();
                    try {
                        startActivityForResult(intent, REQUEST_SELECT_FILE);
                    } catch (ActivityNotFoundException e) {
                        uploadMessage = null;
                        Toast.makeText(getApplicationContext(), "Cannot open file chooser", Toast.LENGTH_LONG).show();
                        return false;
                    }
    
                    return true;
                }
    
    
    
    
            });
    
    
        }
    
        @SuppressLint("NewApi")
        protected void onActivityResult(int requestCode, int resultCode, Intent data){
            if (requestCode == REQUEST_SELECT_FILE) {
                if (uploadMessage == null) return;
                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, data));
                uploadMessage = null;
            }
        }
    
        @Override
        public void onRefresh() {
    
            webView.reload();
            swipeRefreshLayout.setRefreshing(false);
            //adView.loadAd(adRequest);
        }
    
        void showInterAd()
        {
            interstitialAd.setAdListener(new AdListener(){
                @Override
                public void onAdLoaded() {
                    if(interstitialAd.isLoaded())
                        interstitialAd.show();
                }
            });
        }
    
        @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(keyCode== KeyEvent.KEYCODE_BACK)
            {
                if(webView.canGoBack()) {
                    webView.goBack();
                    adView.loadAd(adRequest);
                }else
                {
                    final AlertDialog alert = new AlertDialog.Builder(this).create();
                    alert.setTitle("Exit");
                    alert.setMessage("Are You Sure you want to Exit.");
                    alert.setButton(AlertDialog.BUTTON_POSITIVE,"Yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    });
                    alert.setButton(AlertDialog.BUTTON_NEGATIVE,"Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            alert.dismiss();
                        }
                    });
                    alert.setButton(AlertDialog.BUTTON_NEUTRAL, "Home", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            webView.loadUrl("https://qarya.in/");
                            alert.dismiss();
                        }
                    });
    
                    alert.show();
                }
                return true;
            }
    
            return super.onKeyDown(keyCode, event);
        }
        // Open previous opened link from history on webview when back button pressed
    
        @Override
        // Detect when the back button is pressed
        public void onBackPressed() {
    
            if(webView.canGoBack()) {
                webView.goBack();
    
            } else {
                // Let the system handle the back button
                super.onBackPressed();
            }
        }
    
        public class Callback extends WebViewClient {
    
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
    
                pd.dismiss();
                if(!url.contains("qarya") || url.contains("facebook.com") || url.contains("twitter.com") || url.contains("linkedin.com") ||url.contains("whatsapp.com"))
                {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                }
                else
                {
                    view.loadUrl(url);
                }
    
                return true;
    
    
            }
    
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(getApplicationContext(), "Failed loading app!", Toast.LENGTH_SHORT).show();
                pd.dismiss();
            }
        }
    
    
    }
    

提交回复
热议问题