我正在尝试实施图片下载程序。
以下是我使用的示例: 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)
答案 0 :(得分:1)
您收到此错误的原因是您尝试从另一个线程而不是Main Thread
或Activity Thread
您的问题已在此处得到解答: Android “Only the original thread that created a view hierarchy can touch its views.”
我没有浏览那个人的教程,但您可以轻松创建AsyncTask并致电ImageDownloader
中的doInBackground()
先检索图像,然后在onPostExecute()
中设置您的View图片{1}}
我建议彻底阅读该教程,因为那个人解释了你需要的一切。查看this示例,了解他如何使用ImageDownloader
和ImageAdapter
。