When i try load an Image, Unknown Host Exception in Android
When i try to load an image into android it gives me the following error
06-10 11:08:50.217: WARN/System.err(868): java.net.UnknownHostException: nopsa.hiit.fi
06-10 11:08:50.217: WARN/System.err(868): at java.net.InetAddress.lookupHostByName(InetAddress.java:497)
06-10 11:08:50.217: WARN/System.err(868): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294)
06-10 11:08:50.217: WARN/System.err(868): at java.net.InetAddress.getAllByName(InetAddress.java:256)
06-10 11:08:50.237: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69)
06-10 11:08:50.237: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48)
06-10 11:08:50.272: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322)
06-10 11:08:50.272: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89)
06-10 11:08:50.272: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285)
06-10 11:08:50.277: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267)
06-10 11:08:50.277: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1018)
06-10 11:08:50.297: WARN/System.err(868): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
The file i'm trying to access is http://nopsa.hiit.fi/pmg/viewer/images/thumb_132750728_8f0342f1ac_t.jpg
and the code i used inside onDraw() is following
try {
URL url = new URL(collectable.getIcon_url());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream(开发者_JS百科);
Bitmap img = BitmapFactory.decodeStream(is);
canvas.drawBitmap(img, 100, 100 , null);
} catch (Exception e) {
Log.d(TAG, "Image Load Failed");
e.printStackTrace();
}
If you test on emulator and you are sure you have internet, try to restart the emulator. Sometimes the emulator crashes. I get this error very often.
Permission Denied
occurred because I haven't set the permission in the manifest.
I just needed to add:
<uses-permission android:name="android.permission.INTERNET" />
精彩评论