BlackBerry Bitmap URL web

本文介绍了一种从网络URL获取图片并将其显示为Bitmap的方法。通过创建一个自定义组件BitmapField,可以从指定的URL下载图片资源,并在UI线程中更新显示。此组件适用于需要从互联网加载图标或其他图片的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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) {
		}
	}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值