2011-01-26 56 views
3

我想以编程方式通过URL检索网页缩略图。Android:以编程方式通过URL检索网页缩略图

做了一些搜索和有几种解决方案提出了:

  1. 使用摆幅(JEditorPane中) - 但据我所知&纠正我,如果我错了,这是不可能的使用挥杆Android应用程序。
  2. 使用带有api服务的第三方网站,比如thumbalizr.com - 宁愿不要使用它,因为它水印缩略图,除非我支付(我的应用程序是免费的)。
  3. 不知道是否有可能,但也许使用android浏览器功能呢?可能以隐藏方式访问网址,而活动只显示进度条? :)

任何人都可能提供一些有用的东西?也许更原生的东西?

欣赏任何方向!

回答

0

很好的问题......它可能不够好,但你可以通过android.webkit.WebHistoryItem的getFavicon得到的网络历史记录网站的图标()调用: http://developer.android.com/reference/android/webkit/WebHistoryItem.html AMB

+0

谢谢,但这不是我的意思。我想在用户使用浏览器访问此URL之前检索书签缩略图,这意味着我需要模拟对此页面的请求并以某种方式检索生成的缩略图。 (可以在LauncherPro支付的书签小部件设置中找到示例)。 – 2011-01-26 14:10:09

0

你可以尝试加载使用HttpClient的favicons。 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); 
} 

这里有一个潜在的问题 - ico format is not supported officially by Android。实际上,BitmapFactory在大多数设备上解码ico格式没有任何问题。无论如何,我不确定它会在所有设备上解码ico。

相关问题