Android - WebView接入H5客服&照片/视频上传

webView加载H5,主要是解决上传图片和视频的问题。

1)Activity定义一些常量变量
	private static final int REQUEST_CODE_PERMISSION_CAMERA = 0x03;
    private static final int REQUEST_CODE_CAMERA = 0x02;
    private static final int REQUEST_CODE_ALBUM = 0x01;
    private ValueCallback<Uri[]> uploadMessageAboveL;
    private ValueCallback<Uri> uploadMessage;
    private String mLastPhotoPath,mCurrentPhotoPath;
    private Thread mThread;
2)WebChromeClient、WebViewClient

除了实现onProgressChanged、onShowFileChooser方法外,自定义多个openFileChooser以兼容不同版本。不同版本的api调用不同的方法。

protected WebChromeClient webChromeClient = new WebChromeClient() {
   
        @Override
        public void onProgressChanged(WebView view, int newProgress) {
   
        	//关闭加载中
            if (newProgress >= 100) {
   
                dismissCustomDialog();
            }
        }

        /**
         * API >= 21(Android 5.0.1)回调此方法
         */
        @Override
        public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> valueCallback, FileChooserParams fileChooserParams) {
   
            uploadMessageAboveL = valueCallback;
            uploadPicture();
            return true;
        }

        /**
         * 8(Android 2.2) <= API <= 10(Android 2.3)回调此方法
         */
        private void openFileChooser(ValueCallback<Uri> uploadMsg) {
   
            uploadMessage = uploadMsg;
            uploadPicture();
        }

        /**
         * 11(Android 3.0) <= API <= 15(Android 4.0.3)回调此方法
         */
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
   
            openFileChooser(uploadMsg);
        }

        /**
         * 16(Android 4.1.2) <= API <= 20(Android 4.4W.2)回调此方法
         */
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
   
            openFileChooser(uploadMsg);
        }

    };
protected WebViewClient webViewClient = new WebViewClient() {
   
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
   
            //让一般的http和https协议开头的走正常的页面,而其他的URL则会开启一个Acitity然后去调用原生APP应用
            if (url.startsWith("http") || url.startsWith("https")) {
   
                return super.shouldInterceptRequest(view, url);
            } else {
   
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return null;
            }
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
    // 表明点击网页里面的链接不调用系统浏览器,而是在本WebView中显示
            view.loadUrl(url);//加载需要显示的网页
            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
   //页面完成加载
            super
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值