android WebView中无法响应图片长按事件

android WebView中无法响应图片长按事件

场景:前端反应在浏览器中长按H5页面的图片会调用系统保存相册等方法,但是在android手机的webView上长按图片无反应。

1.首先检查是否添加权限,具体作用暂未弄清

2.在WebView的activity中注册contextMenu,代码
registerForContextMenu(mWebView);
3.重写activity的onCreateContextMenu方法示例:

super.onCreateContextMenu(menu, v, menuInfo);
        MenuItem.OnMenuItemClickListener handler = new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getTitle() == "保存到手机") {
                    new SaveImage().execute();
                } else {
                    return false;
                }
                return true;
            }
        };
        if (v instanceof WebView) {
            WebView.HitTestResult result = ((WebView) v).getHitTestResult();
            if (result != null) {
                int type = result.getType();
                if (type == WebView.HitTestResult.IMAGE_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                    imgurl = result.getExtra();
                    menu.setHeaderTitle("提示");
                    menu.add(0, v.getId(), 0, "保存到手机").setOnMenuItemClickListener(handler);
                }
            }
        }

4.保存图片操作,这里假设使用AsyncTask保存图片

 private String imgurl;

    private class SaveImage extends AsyncTask {
        @Override
        protected String doInBackground(String... params) {
            String result = "";
            try {
                String sdcard = Environment.getExternalStorageDirectory().toString();
                File file = new File(sdcard + "/Download");
                if (!file.exists()) {
                    file.mkdirs();
                }
                int idx = imgurl.lastIndexOf(".");
                String ext = imgurl.substring(idx);
                file = new File(sdcard + "/Download/" + new Date().getTime() + ext);
                InputStream inputStream = null;
                URL url = new URL(imgurl);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(20000);
                if (conn.getResponseCode() == 200) {
                    inputStream = conn.getInputStream();
                }
                byte[] buffer = new byte[4096];
                int len = 0;
                FileOutputStream outStream = new FileOutputStream(file);
                while ((len = inputStream.read(buffer)) != -1) {
                    outStream.write(buffer, 0, len);
                }
                outStream.close();
                result = file.getAbsolutePath();
            } catch (Exception e) {
                result = "保存失败!" + e.getLocalizedMessage();
            }
            return result;
        }

        @Override
        protected void onPostExecute(String result) {
            LogUtil.d(result);
            Uri uri = Uri.fromFile(new File(result));
//            try {
//                MediaStore.Images.Media.insertImage(getContentResolver(), result, "testImg", null);
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            }
            // 通知系统更新相册,例如返回微信快速识别保存图片
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            ToastUtil.showShortToast(result);
        }
    }

你可能感兴趣的:(android WebView中无法响应图片长按事件)