• Android WebView 文件上传点击没有反应的问题


    1。登录等按钮点击没反应

    网页嵌入到webview中,点登录没反应。在微信或者默认浏览器或者手机浏览器都可以
    可以直接换腾讯x5webview试试,我就是这样解决的。

    2.文件上传点击没有反应

    package com.magics.group.gamify;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.annotation.TargetApi;
    import android.app.Activity;
    import android.content.ClipData;
    import android.content.Intent;
    import android.net.Uri;
    import android.net.http.SslError;
    import android.os.Build;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.view.View;
    import android.webkit.SslErrorHandler;
    import android.webkit.ValueCallback;
    import android.webkit.WebChromeClient;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
    
    import static android.view.KeyEvent.KEYCODE_BACK;
    
    public class MainActivity extends AppCompatActivity {
    
        public static final String URL = "***************";
    
        private ValueCallback<Uri> uploadMessage;
        private ValueCallback<Uri[]> uploadMessageAboveL;
        private final static int FILE_CHOOSER_RESULT_CODE = 10000;
    
        WebView mainWeb;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
            // 初始化
            init();
        }
    
        /**
         * 初始化
         */
        public void init() {
    
            mainWeb = findViewById(R.id.mainWeb);
    
            mainWeb.setBackgroundColor(0x00000000);
            mainWeb.setLayerType(View.LAYER_TYPE_HARDWARE,null);
    
            WebSettings webSettings = mainWeb.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setDomStorageEnabled(true);
    
            //设置自适应屏幕,两者合用
            webSettings.setUseWideViewPort(true);
            webSettings.setLoadWithOverviewMode(true);
    
            //其他细节操作
            webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
            webSettings.setLoadsImagesAutomatically(true);
            webSettings.setDefaultTextEncodingName("utf-8");
    
            mainWeb.setWebViewClient(new WebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
                }
    
                @Override
                public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error){
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        mainWeb.getSettings()
                                .setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
                    }
                }
            });
    
            mainWeb.setWebChromeClient(new WebChromeClient() {
    
                // For Android < 3.0
                public void openFileChooser(ValueCallback<Uri> valueCallback) {
                    uploadMessage = valueCallback;
                    openImageChooserActivity();
                }
    
                // For Android  >= 3.0
                public void openFileChooser(ValueCallback valueCallback, String acceptType) {
                    uploadMessage = valueCallback;
                    openImageChooserActivity();
                }
    
                //For Android  >= 4.1
                public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {
                    uploadMessage = valueCallback;
                    openImageChooserActivity();
                }
    
                // For Android >= 5.0
                @Override
                public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {
                    uploadMessageAboveL = filePathCallback;
                    openImageChooserActivity();
                    return true;
                }
            });
    
            mainWeb.loadUrl(URL);
        }
    
        private void openImageChooserActivity() {
            Intent i = new Intent(Intent.ACTION_GET_CONTENT);
            i.addCategory(Intent.CATEGORY_OPENABLE);
            i.setType("image/*");
            startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == FILE_CHOOSER_RESULT_CODE) {
                if (null == uploadMessage && null == uploadMessageAboveL) return;
                Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();
                if (uploadMessageAboveL != null) {
                    onActivityResultAboveL(requestCode, resultCode, data);
                } else if (uploadMessage != null) {
                    uploadMessage.onReceiveValue(result);
                    uploadMessage = null;
                }
            }
        }
    
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {
            if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)
                return;
            Uri[] results = null;
            if (resultCode == Activity.RESULT_OK) {
                if (intent != null) {
                    String dataString = intent.getDataString();
                    ClipData clipData = intent.getClipData();
                    if (clipData != null) {
                        results = new Uri[clipData.getItemCount()];
                        for (int i = 0; i < clipData.getItemCount(); i++) {
                            ClipData.Item item = clipData.getItemAt(i);
                            results[i] = item.getUri();
                        }
                    }
                    if (dataString != null)
                        results = new Uri[]{Uri.parse(dataString)};
                }
            }
            uploadMessageAboveL.onReceiveValue(results);
            uploadMessageAboveL = null;
        }
    
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if ((keyCode == KEYCODE_BACK) && mainWeb.canGoBack()) {
                mainWeb.goBack();
                return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    
    }
    
  • 相关阅读:
    03点云文件常用格式转换(pcd,txt,ply,obj,stl)
    04点云数据结构格式
    vs2015 +ZXing/Zbar的环境配置
    01PCL 点云+vs2015在win10下的环境配置
    07点云的滤波与分割
    gitlabCI/CD部署一个java项目
    k8s 为什么需要数据卷
    gitlab Runner 安装与部署
    gitlab ci/cd介绍
    k8s emptyDir临时数据卷
  • 原文地址:https://www.cnblogs.com/wzqnxd/p/15204806.html
Copyright © 2020-2023  润新知