Reputation: 157
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....
Upvotes: 0
Views: 2384
Reputation: 840
If you are using the method for downloading the image in onCreate() it will behave as a synchronous operation — that is, it will not return control until the image is downloaded — calling it directly will freeze the UI of your activity.This is not allowed in Android 3.0 and later; all synchronous code must be wrapped using an AsyncTask class. Using AsyncTask enables you to perform background tasks in a separate thread and then return the result in a UI thread. That way, you can perform background operations without needing to handle complex threading issues.
To call the DownloadImage() method asynchronously, you need to wrap the code in a subclass of the AsyncTask class, as shown here:
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
protected Bitmap doInBackground(String... urls) {
return DownloadImage(urls[0]);
}
protected void onPostExecute(Bitmap result) {
ImageView img = (ImageView) findViewById(R.id.img);
img.setImageBitmap(result);
}
}
Upvotes: 3
Reputation: 40416
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.
Upvotes: 1
Reputation: 11
Please refer this answer
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This is only thrown for applications targeting the Honeycomb SDK or higher. Applications targeting earlier SDK versions are allowed to do networking on their main event loop threads, but it's heavily discouraged. See the document Designing for Responsiveness.
For more details please visit Android developer site
Upvotes: 0
Reputation: 31466
A good practice in creating responsive applications is to make sure your main UI thread does the minimum amount of work. Any potentially long task that may hang your application should be handled in a different thread (background thread). when you Handle long operation on your main thread you lose responsivness of your android appand if you are using such tasks with network operation you ill get android.os.NetworkOnMainThreadException
Exception throwed to you.
Typical examples of such tasks are network operations, which involve unpredictable delays. Users will tolerate some pauses, especially if you provide feedback that something is in progress, but a frozen application gives them no clue.
In this article, we will create a simple image downloader that illustrates this pattern
you will learn the difference between differents approches and finally you have to keep in mind the right way to do such operations!!
You can see also this simular SO thread that deals with lazy loading operation
Upvotes: 0
Reputation: 20557
You should be using an async task for this, this is a safe guard so your main thread (ui thread ) doesn't hang. They put this in place in 3.0 or 3.1 ill just dig up an old post for an expample for you
HTTP POST request ANDROID 4 (working in 2.3)?
It would be very bad practice to go against android and do networking in the main thread. They have done this for a reason, it will most likely fc if you don't.
Upvotes: 0
Reputation: 7315
Every content has to be downloaded on a separated thread... You can use AsyncTask, Thread...
Upvotes: 0