0

I am trying to load an image from a web url. I found a stack question that answered this, and said to use this code:

try {
  ImageView i = (ImageView)findViewById(R.id.image);
  Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent());
  i.setImageBitmap(bitmap); 
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}

But when I try and run it I get this error:

06-19 14:45:02.598: E/AndroidRuntime(10230): FATAL EXCEPTION: main
06-19 14:45:02.598: E/AndroidRuntime(10230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.beerportfoliopro/com.example.beerportfoliopro.BeerPage}: android.os.NetworkOnMainThreadException
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2357)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.os.Looper.loop(Looper.java:137)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.ActivityThread.main(ActivityThread.java:5226)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.lang.reflect.Method.invokeNative(Native Method)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.lang.reflect.Method.invoke(Method.java:511)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at dalvik.system.NativeStart.main(Native Method)
06-19 14:45:02.598: E/AndroidRuntime(10230): Caused by: android.os.NetworkOnMainThreadException
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.net.URLConnection.getContent(URLConnection.java:190)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at libcore.net.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:175)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at java.net.URL.getContent(URL.java:447)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at com.example.beerportfoliopro.BeerPage.onCreate(BeerPage.java:52)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.Activity.performCreate(Activity.java:5104)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-19 14:45:02.598: E/AndroidRuntime(10230):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)

2 Answers 2

2

You get a NetworkOnMainThreadException. This is because starting with Android 3.0 and above network operations should not be done in the UI thread, but in a separate thread. You could use an AsyncTask, or an IntentService for this purpose.
With other words, that piece of code that connects to internet and downloads the image should run in a AsyncTask or IntentService

Sign up to request clarification or add additional context in comments.

1 Comment

I will, Its to early and wont let me for another few minutes
0

Please try below code

   private Bitmap downloadImage(String imageUrl)
   {

    Bitmap bitmap=null;
    InputStream is;
    try 
    {
    is=openHttp(imageUrl);
    bitmap=BitmapFactory.decodeStream(is);
    is.close();
} 
    catch (Exception e) 
    {
        // TODO: handle exception
}
    return bitmap;
}
private InputStream openHttp(String url) throws IOException
{
    InputStream is=null;
    int res=-1;
    URL u=new URL(url);
    URLConnection con=u.openConnection();
    if(!(con instanceof HttpURLConnection))
        throw new IOException("Not Http Connection");
    try {
        HttpURLConnection httpCon=(HttpURLConnection)con;
        httpCon.setAllowUserInteraction(false);
        httpCon.setInstanceFollowRedirects(true);
        httpCon.setRequestMethod("GET");
        httpCon.connect();
        res=httpCon.getResponseCode();
        if(res==HttpURLConnection.HTTP_OK)
        {
            is=httpCon.getInputStream();
        }
    } 
    catch (Exception e) {
        // TODO: handle exception
    }

    return is;
}

here in downloadImage function rerurn bitmap object. and openHttp return inputstream of image url... check in your application...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.