Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:15:42+00:00 2026-06-17T14:15:42+00:00

When i display image from url, app crashes. What I do wrong? Please help

  • 0

When i display image from url, app crashes. What I do wrong?
Please help me. Thanks!

My Layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <ImageView
        android:id="@+id/imageview"
        android:layout_gravity="center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scaleType="center"
        />
</RelativeLayout>

This is my MainActivity.java.

private String imageFileURL = "http://www.google.com/images/nav_logo114.png";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ImageView myImageView = (ImageView)findViewById(R.id.imageview);
    try {
        URL url = new URL(imageFileURL);
        URLConnection conn = url.openConnection();

        HttpURLConnection httpConn = (HttpURLConnection)conn;
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = httpConn.getInputStream();

            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
            myImageView.setImageBitmap(bitmap);
        }
    } catch (MalformedURLException e1) {
        e1.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

In AndroidManifest.xml, I add

<uses-permission android:name="android.permission.INTERNET" />

But, it still displays errors when running and app crashes.

The following is Logcat

01-22 04:44:19.019: D/AndroidRuntime(4452): Shutting down VM
01-22 04:44:19.019: W/dalvikvm(4452): threadid=1: thread exiting with uncaught exception (group=0x40aba228)
01-22 04:44:19.029: E/AndroidRuntime(4452): FATAL EXCEPTION: main
01-22 04:44:19.029: E/AndroidRuntime(4452): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test/com.test.MainActivity}: android.os.NetworkOnMainThreadException
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.os.Looper.loop(Looper.java:154)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.ActivityThread.main(ActivityThread.java:4945)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at java.lang.reflect.Method.invokeNative(Native Method)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at java.lang.reflect.Method.invoke(Method.java:511)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at dalvik.system.NativeStart.main(Native Method)
01-22 04:44:19.029: E/AndroidRuntime(4452): Caused by: android.os.NetworkOnMainThreadException
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at java.net.InetAddress.lookupHostByName(InetAddress.java:394)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at com.test.MainActivity.onCreate(MainActivity.java:77)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.Activity.performCreate(Activity.java:4531)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
01-22 04:44:19.029: E/AndroidRuntime(4452):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
01-22 04:44:19.029: E/AndroidRuntime(4452):     ... 11 more
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:15:44+00:00Added an answer on June 17, 2026 at 2:15 pm

    You’re performing a network operation on your main UI thread. If your target SDK is 11 (Honeycomb) or higher this will throw a NetworkOnMainThreadException , because this behaviour can block the UI and lead to an unresponsive app.

    You could use an AsyncTask to get around this, loading the data in its doInBackground(..).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app, there is an ImageView that display an image from a URL.
i have a jsf page which displays an image from an url http://ichart.finance.yahoo.com/z?s=^NSEI&t=1d&q=l&l=on&z=l&p=s&a=v&p=s i
My app has to display an image preview from the image data obtained as
I'm having trouble displaying SOME images on my android app. Just normal display image
I used the following code to retrieve an image from a url and display
In my app, I need to display a splash screen.I collect the image from
I'm trying to display an image from a webcam in my app using HJCache
I am trying to display an image from a URL, which may be larger
I have a method in my Android app that can either display from a
I have an App that downloads and displays images from URL's. This works fine

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.