1

我正在尝试实现图像下载器。

这是我使用的示例: http ://android-developers.blogspot.de/2010/07/multithreading-for-performance.html

但不幸的是,我收到了一堆警告,结果是没有显示任何图像。

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)
4

1 回答 1

1

您收到此错误的原因是因为您尝试从另一个线程访问 UI 视图,而不是Main ThreadActivity Thread

您的问题已在此处得到解答: Android“只有创建视图层次结构的原始线程才能触及其视图。”</a>

我还没有完成那个人的教程,但是您可以轻松地创建一个AsyncTask并调用ImageDownloaderindoInBackground()先检索图像,然后将视图的图片设置为onPostExecute()

我建议彻底阅读该教程,因为那个人解释了你需要的一切。检查示例以了解他如何使用ImageDownloaderand ImageAdapter

于 2013-03-08T13:59:21.073 回答