检索位图给出未知的主机异常?

时间:2011-09-30 11:00:18

标签: java android

我正在使用此代码从网上获取位图

  try {
        Bitmap bitmap=null;
        URL imageUrl = new URL(url);
        HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
        conn.setConnectTimeout(30000);
        conn.setReadTimeout(30000);
        InputStream is=conn.getInputStream(); ///Error is pointing me here
        OutputStream os = new FileOutputStream(f);
        Utils.CopyStream(is, os);
        os.close();
        bitmap = decodeFile(f);
        return bitmap;
    } catch (Exception ex){
       ex.printStackTrace();
       return null;
    }

唯一的问题是我一直在犯这种错误。我试图获取位图。 错误指向我:

 InputStream is=conn.getInputStream();


09-30 06:54:32.512: WARN/System.err(11149):     at java.net.InetAddress.lookupHostByName(InetAddress.java:497)
09-30 06:54:32.512: WARN/System.err(11149):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
09-30 06:54:32.512: WARN/System.err(11149):     at java.net.InetAddress.getAllByName(InetAddress.java:256)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
09-30 06:54:32.512: WARN/System.err(11149):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)

2 个答案:

答案 0 :(得分:1)

显然,您的url地址无效。手动将您的网址复制到浏览器窗口,您应该会看到相同的错误。

答案 1 :(得分:1)

这是一个网络错误,可能是您没有添加互联网权限或您的连接网络未正确设置或者您的网址可能错误........... ......