Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: retrieve Webpage thumbnail by URL programmatically

Tags:

android

I would like to retrieve a Webpage thumbnail by URL programmatically.

Done some searching and came up with several solutions:

  1. using Swing (JEditorPane) - but as I understand & correct me if I'm wrong, it's not possible to use swing with Android app.
  2. using a third party site with api services, like thumbalizr.com - prefer not to use that as it watermarks the thumbnail unless I pay (my app is free).
  3. Don't know if it's possible but maybe use the android browser function for that? maybe go to the URL in hidden way while activity shows only a progress bar? :)

Can anyone maybe offer something that works? maybe something more native?

Appreciate any direction!

like image 562
Lior Iluz Avatar asked Dec 06 '25 13:12

Lior Iluz


1 Answers

You can try to load favicons using HttpClient. F.e.:

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
..........

ArrayList<String> list; //list of target urls

HttpClient httpclient = new DefaultHttpClient();
final int size = list.size();
for (int i = 0; i < size; ++i) {
    try {
       String pure_url = extractPureUrl(url);
       HttpResponse response = httpclient.execute(new HttpGet(pure_url + "/favicon.ico"));

       if (response.getEntity() != null) { 
           StatusLine statusLine = response.getStatusLine();
           if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
              ByteArrayOutputStream out = new ByteArrayOutputStream();
              response.getEntity().writeTo(out);
              out.close();

              byte[] image_bytes = out.toByteArray();
              bmp = BitmapFactory.decodeStream(new ByteArrayInputStream(image_bytes));
              if (bmp != null) {
                  //do smth with received bitmap
              }         
           } else {
              response.getEntity().consumeContent();
           }
       }
  } catch (ClientProtocolException e) {
    //log error
  } catch (IOException e) {
    //log error
  }
}

private static final String EXTENDED_HTTP_PREFIX = "http://www.";
private static final String HTTP_PREFIX = "http://";
/** Converts http://abc.com/xxx and http://www.abc.com/xxx to http://abc.com */
private static String extractPureUrl(String srcUrl) {
    String sw = srcUrl.toLowerCase();
    String s = sw.startsWith(EXTENDED_HTTP_PREFIX) 
   ? sw.substring(EXTENDED_HTTP_PREFIX.length()) 
   : sw.startsWith(BookmarkInfo.HTTP_PREFIX) 
      ? sw.substring(BookmarkInfo.HTTP_PREFIX.length()) 
      : sw;
    int n = s.indexOf('/');
    if (n == -1) return srcUrl;
    return HTTP_PREFIX + s.substring(0, n);
}

There is a potential problems here - ico format is not supported officially by Android. In practice, BitmapFactory decodes ico-format on most devices without any problems. Anyway, I am not sure, that it will decode ico on abs all devices.

like image 69
dvpublic Avatar answered Dec 09 '25 02:12

dvpublic