Custom component for Bitmap from web URL
usage:
BitmapField icon = new BitmapField("http://www.google.com/")
add(icon);
完整代码:BitmapField.zip
private void getBitmap(final String url) {
new Thread(new Runnable() {
public void run() {
HttpConnection connection = null;
InputStream inputStream = null;
try {
connection = (HttpConnection) Connector.open(url, Connector.READ, true);
inputStream = connection.openInputStream();
final byte[] result = inputStreamToByte(inputStream);
int responseCode = connection.getResponseCode();
if (responseCode != HttpConnection.HTTP_OK) {
logger.info("HTTP response code: " + responseCode);
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
callback(result);
}
});
} catch (final Exception e) {
logger.error(e);
} finally {
try {
inputStream.close();
inputStream = null;
connection.close();
connection = null;
} catch (Exception e) {
logger.error(e);
}
}
}
}).start();
}
public void callback(final byte[] data) {
try {
bitmap = EncodedImage.createEncodedImage(data, 0, data.length);
setImage(bitmap);
} catch (final Exception e) {
}
}
本文介绍了一种从网络URL获取图片并将其显示为Bitmap的方法。通过创建一个自定义组件BitmapField,可以从指定的URL下载图片资源,并在UI线程中更新显示。此组件适用于需要从互联网加载图标或其他图片的应用场景。
2199

被折叠的 条评论
为什么被折叠?



