问题
I have to develop a url which involves downloading image from url and show in blackberry stimulator..Can anyone help me in this regard???
回答1:
This code 'll connect the given URL and returns Bitmap object
public static Bitmap connectServerForImage(String url) {
HttpConnection httpConnection = null;
DataOutputStream httpDataOutput = null;
InputStream httpInput = null;
int rc;
Bitmap bitmp = null;
try {
httpConnection = (HttpConnection) Connector.open(url);
rc = httpConnection.getResponseCode();
if (rc != HttpConnection.HTTP_OK) {
throw new IOException("HTTP response code: " + rc);
}
httpInput = httpConnection.openInputStream();
InputStream inp = httpInput;
byte[] b = IOUtilities.streamToBytes(inp);
EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length);
return hai.getBitmap();
} catch (Exception ex) {
System.out.println("URL Bitmap Error........" + ex.getMessage());
} finally {
try {
if (httpInput != null)
httpInput.close();
if (httpDataOutput != null)
httpDataOutput.close();
if (httpConnection != null)
httpConnection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return bitmp;
}
U can create a bimapfield and asign this bitmap as
BitmapField bmpFld1=new BitmapField(connectServerForImage(Url));
回答2:
For base 64 string decoding
try {
mapaByte = Base64InputStream.decode(imagenB64);
Bitmap mapa64 = Bitmap.createBitmapFromBytes(mapaByte, 0, -1, 1);
mapa.setBitmap(mapa64);
}
catch (Exception e) {}
来源:https://stackoverflow.com/questions/4310572/download-and-show-image-on-a-blackberry