1

I'm trying to implement an Image downloader.

Here is the example that I used: http://android-developers.blogspot.de/2010/07/multithreading-for-performance.html

But unfortunately I'm getting a bunch of warnings and the result is that no image gets displayed.

https://www.dropbox.com/s/jpstbcehoii2xt7/snipp2.PNG

    runOnUiThread(new Runnable() {
         public void run() {
            final Handler uiHandler = new Handler();    
            Timer myTimer = new Timer(); // Timer erzeugen
            myTimer.schedule(new TimerTask() {          
                @Override           
                public void run() {                     
                             try{
                                  imageDownloader.download("http://osthessen-news.de/Media/13/03/News130308_10_DSC_2375.jpg_Thumbnail0.jpg", (ImageView) mainImage1);                   
                                } catch (Exception e) {
                                    e.printStackTrace(); 
                                }
                             }
        }, 0L, 60L * 1000); //Intervall = 60000 Millisekunden, 0 Millisekunden bis zum ersten Start.

    }});

03-08 14:22:14.577: W/ImageDownloader(12411): Error while retrieving bitmap from http://osthessen-news.de/Media/13/03/News130308_10_DSC_2375.jpg_Thumbnail0.jpg
03-08 14:22:14.577: W/ImageDownloader(12411): android.os.NetworkOnMainThreadException
03-08 14:22:14.577: W/ImageDownloader(12411):   at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
03-08 14:22:14.577: W/ImageDownloader(12411):   at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
03-08 14:22:14.577: W/ImageDownloader(12411):   at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
03-08 14:22:14.577: W/ImageDownloader(12411):   at java.net.InetAddress.getAllByName(InetAddress.java:214)
03-08 14:22:14.577: W/ImageDownloader(12411):   at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
03-08 14:22:14.577: W/ImageDownloader(12411):   at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-08 14:22:14.577: W/ImageDownloader(12411):   at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-08 14:22:14.577: W/ImageDownloader(12411):   at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-08 14:22:14.577: W/ImageDownloader(12411):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-08 14:22:14.577: W/ImageDownloader(12411):   at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
luQ
  • 519
  • 1
  • 10
  • 25
  • 1
    It's better to include some part of your code as well as the _Logcat_ output as text rather than a picture. – Sam R. Mar 08 '13 at 10:24
  • I think the solution is to create a new Thread, but im not sure how and where :/ – luQ Mar 08 '13 at 12:14
  • I think the `Image Downloader` creates an `AsyncTask` by itself so you don't need to do it. But at least add your complete `Activity` code so somebody can help you. Add your _LOGCAT_ as text to your question. – Sam R. Mar 08 '13 at 12:25

1 Answers1

1

The reason that you are getting this error is because you are trying to access a UI view from another thread rather than Main Thread or Activity Thread

Your problem has already been answered here: Android “Only the original thread that created a view hierarchy can touch its views.”

I haven't gone through that guy's tutorial but you can easily create an AsyncTask and call the ImageDownloader in doInBackground() to retrieve the image first and then set your View's picture in onPostExecute()

I recommend to read that tutorial thoroughly cuz that guy explains everything you need. Check this sample to see how he uses the ImageDownloader and ImageAdapter.

Community
  • 1
  • 1
Sam R.
  • 16,027
  • 12
  • 69
  • 122
  • Thx, but yet, i get the old warning: "android.os.StrictMode&AndroidBlockGuardPolic.onNetwork(StrictM – luQ Mar 08 '13 at 14:29
  • I have checked your update. DON'T DO THAT. You are calling the `ImageDownloader` on UI thread which is terrible. I will update my answer. – Sam R. Mar 08 '13 at 14:39
  • The ImageDownloader itself creates an AsyncTask! – luQ Mar 08 '13 at 14:46
  • Forget about the warning for now. But you have another error in your Log. `Error while retrieving bitmap from ...`. What the hell is that? :) – Sam R. Mar 08 '13 at 14:57