0

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();
   Bitmap img = BitmapFactory.decodeStream(is);                 
   canvas.drawBitmap(img, 100, 100 , null);
} catch (Exception e) {
   Log.d(TAG, "Image Load Failed");
   e.printStackTrace();
}
dinesh707
  • 12,106
  • 22
  • 84
  • 134
  • And you do ofcourse have internet connection on the device ? Just checking :-) – Karl-Bjørnar Øie Jun 10 '11 at 11:26
  • Does collectable.getIcon_url() return a right string? Does it work with another, mock urls? You gotta test this. – Egor Jun 10 '11 at 11:27
  • Actually, im running it on emulator?. I thought, since the computer has internet emulator has it too – dinesh707 Jun 10 '11 at 11:28
  • @Egor: I printed out the URL and checked it on web browser, it works – dinesh707 Jun 10 '11 at 11:29
  • Try to test your downloader method with a mock url to understand, whether it's a current url's issue or not. And if the computer has internet, emulator will have it too automatically. – Egor Jun 10 '11 at 11:31

2 Answers2

2

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.

Plamen Nikolov
  • 4,177
  • 3
  • 23
  • 24
  • Yes, try restarting, I get this often too. Enable snapshots so it takes less time to cycle the emu. – dmon Jun 10 '11 at 11:31
  • i restarted it 3 times, no luck. But when i goto internet through emulator it works – dinesh707 Jun 10 '11 at 11:44
  • 1
    Replace the host name nopsa.hiit.fi with its real IP address. Do you have a connection? May the problem is in the android's dns cache. is this a new domain? – Plamen Nikolov Jun 10 '11 at 11:50
  • 1
    @Pepi, after i changed the host name to IP adress now it gives me a Permission Denied error. Any way the previous problem seems to be solved. – dinesh707 Jun 10 '11 at 12:29
  • I found the answer to Permission Denied as well. It was at http://stackoverflow.com/questions/5168494/android-socketexception-thrown-when-attempting-httpurlconnection-getoutputstream – dinesh707 Jun 10 '11 at 12:36
  • @Pepi, if you post your answer separately, I can accept it as the answer – dinesh707 Jun 10 '11 at 12:36
2

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" /> 
Eran
  • 387,369
  • 54
  • 702
  • 768
dinesh707
  • 12,106
  • 22
  • 84
  • 134