我是Android编程和线程的新手。我想从远程服务器获取图片并显示它。 (迄今为止的作品^^) 但图片来自相机,所以我需要一个新的,只要我展示我之前下载的一个。这意味着,线程永远不应该停止抓图片。 (只要活动存在。)
另外我只想建立到服务器的一个连接,然后只需执行HTTP-gets。所以我必须有一个Thread可以使用的参数“连接”。Android - 反复执行线程
为了得到一个想法 - 它应该工作是这样的(但显然事实并非如此):
private class DownloadImageTask extends AsyncTask<URLConnection, Void, Bitmap> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
private URLConnection connection = null;
protected Bitmap doInBackground(URLConnection...connection) {
this.connection = connection[0];
return getImageFromServer(connection[0]);
}
protected void onPostExecute(Bitmap result) {
pic.setImageBitmap(result);
this.doInBackground(connection);
}
}
谢谢,这帮了我很多! – user1271544 2012-03-16 13:08:59
还有一件事情...没有'c.close();'我如何关闭'URLConnection'?到目前为止我已经找到了一些东西。 – user1271544 2012-03-16 13:21:53
嗯,那么你应该得到'InputStream'上应该有'.close()'。你应该总是关闭你打开的东西 - 这就是为什么我添加了一个关闭:) – zapl 2012-03-16 13:44:44