关于android webview 的那些坑

来源:互联网 发布:webrtc 源码介绍 编辑:程序博客网 时间:2024/05/22 05:18

近日做项目的时候使用webview 遇到一些麻烦。

第一条:H5页面选择图片时,点击无效。(无法选择照片)

第二条:当修复完第一条bug时,出现新bug,如果选择照片时,什么都不选返回,会出现无法点击的情况。

第三条:如何打开PDF格式的文件


首先第一个问题,h5中调用摄像头的代码是:

<input accept="image/*" capture="camera" id="imgFile" name="imgFile" type="file">。

在PC端以及手机浏览器可以正常点击并且选择图片,但是嵌入到APP的webview中,点击没有任何反应。

解决方案  重写 WebChromeClient里面的方法


mWebView.setWebChromeClient(new WebChromeClient() {            //配置权限(同样在WebChromeClient中实现)            @Override            public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {                callback.invoke(origin, true, false);                super.onGeolocationPermissionsShowPrompt(origin, callback);            }            @Override            public void onProgressChanged(WebView view, int newProgress) {                            }            // For Android < 3.0            public void openFileChooser(ValueCallback<Uri> uploadMsg) {                mUploadMessage = uploadMsg;                showOptions();            }            // For Android  > 4.1.1            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {                mUploadMessage = uploadMsg;                showOptions();            }            // For Android > 5.0支持多张上传            @Override            public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams                    fileChooserParams) {                mUploadCallbackAboveL = uploadMsg;                showOptions();                return true;            }            @Override            public void onReceivedTitle(WebView view, String title) {                super.onReceivedTitle(view, title);                tvTitle.setText(title);            }        });

第二个问题,是在第一个问题修复完的情况下,我已经可以选择照片了,但是在选择的过程中,点击了取消或者返回,无法重复回调onShowFileChooseropenFileChooser方法,就会有无法选择图片的bug。

      logcat 

打印出log:

Attempted to finish an input event but the input event receiver has already been disposed

解决方案:

    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        //图片上传        if ((null == mUploadMessage) && (mUploadCallbackAboveL == null)) {            return;        }        if (requestCode == PHOTO_REQUEST_GALLERY) {            if (data != null) {                ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);                ImageItem imageItem = images.get(0);//                File file = new File(imageItem.path);//                //压缩//                File newFile = CompressHelper.getDefault(this).compressToFile(file);//                //压缩转为bitmap//                Bitmap bitmap = BitmapFactory.decodeFile(newFile.toString());                Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), ImageUtils.ratio(imageItem.path, 950, 1280), null, null));                if (mUploadCallbackAboveL != null) {                    Uri[] uris = new Uri[]{uri};                    mUploadCallbackAboveL.onReceiveValue(uris);                    mUploadCallbackAboveL = null;                } else {                    mUploadMessage.onReceiveValue(uri);                    mUploadMessage = null;                }            }else{            }        }        if (resultCode==RESULT_CANCELED){            cancelFilePathCallback();        }    }

    /**     *取消mFilePathCallback回调     */    private void cancelFilePathCallback() {        if (mUploadMessage != null) {            mUploadMessage.onReceiveValue(null);            mUploadMessage = null;        } else if (mUploadCallbackAboveL != null) {            mUploadCallbackAboveL.onReceiveValue(null);            mUploadCallbackAboveL = null;        }    }


第三个问题 如何打开PDF 文件,这个我们的需求是直接用Webview打开PDF 文件,或者跳转到手机浏览器下载 ,我的做法是 
重写 WebViewClient 这个类的方法 
shouldOverrideUrlLoading 
 mWebView.setWebViewClient(new WebViewClient() {            @Override            public boolean shouldOverrideUrlLoading(WebView view, final String url) {                Log.e("url","url="+url);                if (url.contains("tel")) {                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(FlashLoanH5DetailActivity.this);                    builder.setTitle("温馨提示").setMessage("是否询问客服?").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialog, int which) {                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));                            //android6.0后可以对权限判断                            if (ActivityCompat.checkSelfPermission(FlashLoanH5DetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {                                Toast.makeText(FlashLoanH5DetailActivity.this, "请在应用管理中打开“电话”访问权限!", Toast.LENGTH_SHORT).show();                                ActivityCompat.requestPermissions(FlashLoanH5DetailActivity.this, new String[]{Manifest.permission.CAMERA}, 1);//权限申请                                return;                            }                            startActivity(intent);                        }                    }).show();                    return true;                }                if (url.contains(".pdf")){                    Intent intent = new Intent();                    intent.setAction("android.intent.action.VIEW");                    Uri content_url = Uri.parse(url);                    intent.setData(content_url);                    startActivity(intent);                }                return super.shouldOverrideUrlLoading(view, url);            }        });

以上内容参考文章 

http://www.jianshu.com/p/e4fe8871af78