从WebView中的网站保存图像? [英] Save images from website inside a WebView?

查看:141
本文介绍了从WebView中的网站保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我四处搜索,大多数答案都与静态图像有关,即只有一个图像,而URL已经预先定义。

I searched around and most of the answers relate to images that are static, i.e just the one image and the URL is already pre-defined.

我设置了一个WebView,可以将用户带到一个充满照片的网站。我希望用户能够按住图像并获得将其保存到手机的选项。那可能吗?我当然不知道URL或任何东西,因为图像每天都在变化,而且有很多。

I have a WebView set up to take users to a website full of photos. I want the user to be able to hold down on an image and get the option to save it to their phone. Is that possible? I of course don't know the URL or anything because the images change every day and there are a lot of them.

网站上的图片都有自己的页面如果这有任何区别。它只是一个带图像的网站。无需身份验证。就像在桌面上一样,您可以右键单击并保存图像,我可以使用WebView执行此操作吗?必须有一种方法,因为它可以在默认浏览器中使用。

The images on the website all have their own page if that makes any difference. It's just a website with images. No authentication is required. Just like on a Desktop you can right click and save an image, can I do that with a WebView? There must be a way since it's possible in the default browser.

编辑:我一直在使用一个名为'Reddit is fun'的应用程序,它具有此功能,并且在面对它,它看起来像一个带操作栏的简单WebView,所以我认为它是可能的。

I've been using an app called 'Reddit is fun' that has this function and on the face of it, it looks like a simple WebView with action bar so I presume it's possible.

我很感激任何帮助。

推荐答案

对,我弄清楚了。

使用上下文菜单我可以按住图像来拉取它的URL。然后我可以让它使用DownloadManager启动下载。这一点是不言自明的,所以我将显示获取图像URL的代码。

Using a context menu I can hold down on an image to pull it's URL. Then I can have it initiate a download using DownloadManager. That bit is self-explanatory so I'll show the code to getting the image URL.

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    // Confirm the view is a webview
    if (v instanceof WebView) {                
        WebView.HitTestResult result = ((WebView) v).getHitTestResult();

        if (result != null) {
            int type = result.getType();

            // Confirm type is an image
            if (type == WebView.HitTestResult.IMAGE_TYPE || type == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {
                String imageUrl = result.getExtra();
                Toast.makeText(this, imageUrl, Toast.LENGTH_LONG).show();

            }
        }
    }
}

这篇关于从WebView中的网站保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆