android:webview长按图片下载

本文介绍了如何在Android的WebView中实现用户长按图片时能够进行下载的操作,包括对WebView的配置,利用ImageLoader库下载图片到特定文件夹,以及详细说明了保存文件的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对webview做以下设置

 public static void loadWebImage(WebView webView, final Context mContext)
    {
        webView.setOnLongClickListener(new View.OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v)
            {
                WebView.HitTestResult result = ((WebView) v).getHitTestResult();
                if (null == result)
                {
                    return false;
                }
                int type = result.getType();
                if (type == WebView.HitTestResult.UNKNOWN_TYPE)
                {
                    return false;
                }
                if (type == WebView.HitTestResult.EDIT_TEXT_TYPE)
                {
                    //let TextViewhandles context menu return true;
                }
                switch (type)
                {
                    case WebView.HitTestResult.PHONE_TYPE: // 处理拨号
                        break;
                    case WebView.HitTestResult.EMAIL_TYPE: // 处理Email
                        break;
                    case WebView.HitTestResult.GEO_TYPE: // TODO
                        break;
                    case WebView.HitTestResult.SRC_ANCHOR_TYPE: // 超链接
                        // Log.d(DEG_TAG, "超链接");
                        break;
                    case WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE:
                        break;
                    case WebView.HitTestResult.IMAGE_TYPE: // 处理长按图片的菜单项
                        String imgurl = result.getExtra();
                        DialogUtil.showNoTipTwoBnttonDialog(mContext, mContext.getResources().getString(R.string.person_sure_to_save),
                                mContext.getResources().getString(R.string.money_tranfer_cancel),
                                mContext.getResources().getString(R.string.reset_password_sure),
                                imgurl,
                                NotiTag.TAG_MONEY_DIALOG_LEFT_BINDCARD, NotiTag.TAG_SAVE_WEB_IMAGE);
                        break;
                    default:
                        break;
                }

                return true;
            }
        });
    }

使用ImageLoader下载图片到指定文件夹

  /***
     * 保存图片到SD卡
     */
    public static void saveImgToSD(final Activity context, String iamgeUrl)
    {


        DisplayImageOptions displayImageOptions = new DisplayImageOptions.Builder()
                .cacheOnDisk(true).cacheInMemory(true).build();
        ImageLoader.getInstance().loadImage(iamgeUrl, displayImageOptions, new ImageLoadingListener()
        {


            @Override
            public void onLoadingStarted(String s, View view)
            {

            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason)
            {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
            {

                String fileName = new HashCodeFileNameGenerator().generate(imageUri) + ".png";
                String path = FileSystemManager.getCacheImgFilePath(context);
                File saveImageFile = GeneralUtils.saveFile(loadedImage, fileName, path);
                if (saveImageFile != null)
                {
                    ToastUtil.makeText(context, String.format("%s%s/%s", "图片保存到", path, fileName));
                    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri uri = Uri.fromFile(saveImageFile);
                    intent.setData(uri);
                    context.sendBroadcast(intent);//这个广播的目的就是更新图库,发了这个广播进入相册就可以找到你保存的图片了!,记得要传你更新的file哦
                }
                else
                {
                    ToastUtil.makeText(context, "图片保存失败");
                }
                //对bitmap进行垃圾回收
                loadedImage.recycle();
            }

            @Override
            public void onLoadingCancelled(String imageUri, View view)
            {

            }
        });
    }

保存文件


    public static File saveFile(Bitmap bm, String fileName, String path)  {

        File myCaptureFile = null;
        try {
            String subForder = path;
            File foder = new File(subForder);
            if (!foder.exists()) {
                foder.mkdirs();
            }
            myCaptureFile = new File(subForder, fileName);
            if (!myCaptureFile.exists()) {
                myCaptureFile.createNewFile();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            bos.flush();
            bos.close();
        }catch (IOException e){

            e.printStackTrace();
        }

        return myCaptureFile;

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值