0

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.

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.

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.

Edit: 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.

I would appreciate any help of the matter.

RED_
  • 2,997
  • 4
  • 40
  • 59

2 Answers2

1

Right I figured it out.

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();

            }
        }
    }
}
RED_
  • 2,997
  • 4
  • 40
  • 59
0

I think a nice solution would be to use javascript in conjuction with WebView.addJavascriptInterface(Object,String) in order to make a"bridge" between rendered images and your Activity. A nice post about this approach is here.

Essentially you should be able to intercept an hold event over each image url via javascript, then you should send such information to your Activity via the method above in order to do whatever you want.

a.bertucci
  • 12,142
  • 2
  • 31
  • 32
  • Looking at this at the moment: http://stackoverflow.com/questions/7963164/context-menu-for-image-saving-in-an-android-webview I'll accept your answer if it fits. – RED_ Apr 20 '13 at 10:43