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 1

2

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(..).

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

1 Comment

Thank you. I have already solved this question. I also see stackoverflow.com/questions/6343166/…

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.