android:webview长按图片下载

来源:互联网 发布:淘宝店怎么免费推广 编辑:程序博客网 时间:2024/06/05 00:45

对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;    }
0 0
原创粉丝点击