i got a little problem with downloading images from the web to my android project. Everytime i try to connect to the internet my app just keeps crashing.
Yes i added the permission to the Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
The logCat says this:
06-02 12:15:36.520: E/AndroidRuntime(13947): FATAL EXCEPTION: main
06-02 12:15:36.520: E/AndroidRuntime(13947): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.MyAppActivity}: android.os.NetworkOnMainThreadException
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.os.Looper.loop(Looper.java:137)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.ActivityThread.main(ActivityThread.java:4424)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 12:15:36.520: E/AndroidRuntime(13947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-02 12:15:36.520: E/AndroidRuntime(13947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-02 12:15:36.520: E/AndroidRuntime(13947): at dalvik.system.NativeStart.main(Native Method)
06-02 12:15:36.520: E/AndroidRuntime(13947): Caused by: android.os.NetworkOnMainThreadException
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
06-02 12:15:36.520: E/AndroidRuntime(13947): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.net.URLConnection.getContent(URLConnection.java:194)
06-02 12:15:36.520: E/AndroidRuntime(13947): at java.net.URL.getContent(URL.java:447)
06-02 12:15:36.520: E/AndroidRuntime(13947): at com.my.app.MyAppActivity.onCreate(MyAppActivity.java:25)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.Activity.performCreate(Activity.java:4465)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-02 12:15:36.520: E/AndroidRuntime(13947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-02 12:15:36.520: E/AndroidRuntime(13947): ... 11 more
Spent almost an hour now trying to figure out what’s the problem….
you have to use AsyncTask to download image from server because your main thread can’t handle it.And you are trying to download image in main thread.so NetworkOnMainThreadException occure.So download image in seperate thread.
See this Example