Android:网页长按识别二维码/下载图片

场景

产品的网页内含有二维码的图片,需要长按识别二维码,也可下载该图片

实现

  • 需要依赖ZXing来识别二维码
  • 需要Glide的bitmap读取
  • 保存图片的工具类,判断是否是base64类型的图片
//在webView长按时触发
 /*长按识别*/
 webView.setOnLongClickListener(object : View.OnLongClickListener {
                    override fun onLongClick(v: View?): Boolean {
                        val result = webView.hitTestResult
                        if (!result.checkNotNull()) {
                            return false;
                        }
                        val type = result.getType();
                        when (type) {
                        // 超链接
                            com.tencent.smtt.sdk.WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE,/*带有链接的图片类型*/com.tencent.smtt.sdk.WebView.HitTestResult.IMAGE_TYPE -> {
                                // 处理长按图片的菜单项
                                val coverUrl = result.getExtra();
                                if (URLUtil.isValidUrl(coverUrl) || coverUrl.startsWith("data:image")) {
                                    UiUtils.webViewLongClick(fragment, fragment.context!!, coverUrl)
                                    return true
                                }
                            }
                        }
                        return false
                    }
                })



    /*网页长按弹窗(识别二维码,下载图片)*/
    private fun openSelect(fragment: BaseFragment, context: Context, coverUrl: String, isYouZan: Boolean = false) {
        //城市列表
        val dialog = BottomSheetDialog(context)
        val cityLayout = LayoutInflater.from(context).inflate(R.layout.dialog_select_qrcode_down_img, null)
        cityLayout.findViewById(R.id.tv_qrcode).setOnClickListener {
            //图片url
            Glide.with(context.getApplicationContext()).asBitmap().load(coverUrl)
                    .into(object : SimpleTarget() {
                        override fun onResourceReady(resource: Bitmap?, transition: Transition?) {
                            CodeUtils.analyzeBitmap(resource, object : CodeUtils.AnalyzeCallback {
                                override fun onAnalyzeSuccess(mBitmap: Bitmap, result: String) {
                                    if (isYouZan) {
                                        IntentUtils.openYouZanAboutWeb(context, result)
                                    } else {
                                        fragment.start(WebFragment.newInstance("", result))
                                    }
                                }

                                override fun onAnalyzeFailed() {
                                    "未识别到二维码".toast()
                                }

                            })
                            dialog.dismiss()
                        }
                    })
        }
        cityLayout.findViewById(R.id.tv_download).setOnClickListener {
            if (coverUrl.startsWith("data:image")) {
                var fileName: String? = null
                if (MimeTypeMap.getFileExtensionFromUrl(coverUrl) == "") {
                    fileName = System.currentTimeMillis().toString() + ".jpg"
                } else {
                    fileName = System.currentTimeMillis().toString() + "." + MimeTypeMap.getFileExtensionFromUrl(coverUrl)
                }
                val file = BitmapUtils.saveBitmapFile(AndroidtoJs.stringToBitmap(coverUrl), DownLoadImageRunnable.SAVE_DOWNLOAD_IMAGE_PATH + fileName)
                if (file.checkNotNull()) {
                    "已保存".toast()
                } else {
                    "保存失败".toast()
                }
            } else {
                ImageUtil.downLoadImg(context, coverUrl)
            }
            dialog.dismiss()
        }
        dialog.setContentView(cityLayout)
        dialog.show()
    }

附:打开系统浏览器

    //    跳转浏览器下载
    private fun downloadByBrowser(context: Context, url: String) {
        val intent = Intent(Intent.ACTION_VIEW)
        intent.addCategory(Intent.CATEGORY_BROWSABLE)
        intent.data = Uri.parse(url)
        context.startActivity(intent)
    }

你可能感兴趣的:(Android:网页长按识别二维码/下载图片)