每當我的應用進入線上下載圖片的佈局時,設備將掛起,需要等待下載完成才能移動。在線下載圖片時黑莓設備掛起
我做了一些研究。他們建議下載另一個Thread
。但是,我不明白如何在另一個Thread
中實現下載功能。
這是我的代碼來調用下載圖像功能。
Main.getUiApplication().invokeLater(new Runnable() {
public void run() {
for (j = 0; j < imagepath.length; j++) {
if (!imagepath[j].toString().equals("no picture")
&& Config_GlobalFunction.isConnected()) {
loader = new Util_LazyLoader(imagepath[j],
new Util_BitmapDowloadListener() {
public void ImageDownloadCompleted(
Bitmap bmp) {
imagebitmap[j] = bmp;
invalidate();
}
});
loader.run();
}
}
}
}, 500, false);
而且lazyloader
public class Util_LazyLoader implements Runnable {
String url = null;
Util_BitmapDowloadListener listener = null;
public Util_LazyLoader(String url, Util_BitmapDowloadListener listener) {
this.url = url;
this.listener = listener;
}
public void run() {
Bitmap bmpImage = getImageFromWeb(url);
listener.ImageDownloadCompleted(bmpImage);
}
private Bitmap getImageFromWeb(String url) {
HttpConnection connection = null;
InputStream inputStream = null;
EncodedImage bitmap;
byte[] dataArray = null;
try {
connection = (HttpConnection) (new ConnectionFactory())
.getConnection(url + Database_Webservice.ht_params)
.getConnection();
int responseCode = connection.getResponseCode();
if (responseCode == HttpConnection.HTTP_OK) {
inputStream = connection.openDataInputStream();
dataArray = IOUtilities.streamToBytes(inputStream);
}
} catch (Exception ex) {
} finally {
try {
inputStream.close();
connection.close();
} catch (Exception e) {
}
}
if (dataArray != null) {
bitmap = EncodedImage.createEncodedImage(dataArray, 0,
dataArray.length);
return bitmap.getBitmap();
} else {
return null;
}
}
}
我需要它的幫助,因爲我在網絡不熟悉。
好吧,它確實爲listfield而不是其他領域。例如,我得到一個位圖字段,我顯示本地圖像,然後在線程中運行下載。之後,我稱之爲'imagebitmap [j] = bmp; invalidate();'位圖字段的圖像不會像列表字段中那樣改變。 – 2012-08-07 09:42:47
@AlanLai,我想知道如果問題是你正在使用變量'j'來確定'imagebitmap []'中的哪個位圖存儲。 'Util_BitmapDowloadListener'可能需要傳遞給索引'j'。你也可以發佈那個界面嗎? – Nate 2012-08-07 10:31:40
我解決了使用你的代碼,包括'同步' – 2012-08-08 06:58:44