我是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