Toni Kanoni
Toni Kanoni

Reputation: 2317

Android WebView: Save image from site with authentication

Thanks to stackoverflow, I managed to implement a webview where I can save the images via a long click context menu/HitTestResult. So when I get the URL of an image, I do something like this:

    URL url = new URL(yourImageUrl);
      InputStream is = (InputStream) url.getContent();
      byte[] buffer = new byte[8192];
      int bytesRead;
      ByteArrayOutputStream output = new ByteArrayOutputStream();
      while ((bytesRead = is.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
      }

then put the output.toByteArray() into a FileOutputStream;

For "normal" sites this works fine, images are stored on the sdcard.

But I still don't know (and I did extensive search on this :-( how to download an image of a site which requires some kind of authentication. For example, I enter the site and input a username/password into a form (some server side language like PHP), which brings me to some pictures. The webview has no problem in logging in and displaying everything. But I can't save images, since the authentication - which is present in the webview - is not present in my image saving mechanism.

With the above code I simply get a FileNotFoundException on the URL.getContent(). Then I tried to use HttpClient and HttpGet/HttpResponse, where the response is always code 403.

My question: How can I access/download/authenticate to get images of protected areas (may it be through server side language or basic authentication).

I mean... it's all there, displayed correctly and authenticated in the WebView :-( But there's no connection between the content of the WebView and my URL/Http Request downloading efforts. Can the webview somehow share it's authentication state?

I even thought about fetching the images from the WebView cache, because it's all there. (but I wasn't able to find out how to do this either...). Is there no mechanism to get the image somehow directly out of the WebView?

I would be thankful for any kind of help!

Upvotes: 2

Views: 913

Answers (1)

EwyynTomato
EwyynTomato

Reputation: 4077

If the authentication method is using cookies, the method below may work:

First, sync cookies on your webview before loading url:

CookieSyncManager cookieSyncManager = 
                    CookieSyncManager.createInstance(webView.getContext());
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
cookieSyncManager.sync();

Next, Upon requesting your image, you can put cookies on the domain to your http request headers and get the content from HttpResponse:

String cookies = CookieManager.getInstance().getCookie(yourImageUrl);
HttpClient httpClient    = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet          = new HttpGet(yourImageUrl);
httpGet.setHeader("Cookie", cookies);

HttpResponse httpResponse = httpClient.execute(httpGet, localContext);
HttpEntity httpEntity     = httpResponse.getEntity();
InputStream is            = httpEntity.getContent();

Upvotes: 0

Related Questions