BlackBerry Bitmap URL web

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) {
		}
	}
 

你可能感兴趣的:(thread,Web,Google,BlackBerry)