在BlackBerry 5.0中显示静态Google地图图像

时间:2011-01-23 18:01:20

标签: image google-maps blackberry png httpconnection

我有一个非常有趣的问题需要解决:

我正在获取一张静态谷歌地图图片,其网址为this

我尝试了几种获取此信息的方法: 将“远程资源”作为ByteArrayOutputStream获取,将Image存储在模拟器的SD中,等等......但每次吓坏时,我都会得到 IlegalArgumentException 。 / p>

我总是得到200个http响应和正确的MIME类型(“image / png”),但无论如何:获取图像并将其转换为Bitmap,或者将图像存储在SD中并稍后读取;我得到相同的结果......文件总是腐败。

我真的相信它的编码问题,或读取方法(类似于这个):

public static Bitmap downloadImage(InputStream inStream){
  byte[] buffer = new byte[256];
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  while (inStream.read(buffer) != -1){
    baos.write(buffer);
  }
  baos.flush();
  baos.close();

  byte[] imageData = baos.toByteArray();
  Bitmap bi = Bitmap.createBitmapFromBytes(imageData, 0, imageData.length, 1);
  //Bitmap bi = Bitmap.createBitmapFromBytes(imageData, 0, -1, 1);
  return bi;
}

唯一想到的是imageData.lenght(在响应中,内容长度为:6005),但我真的无法想出这个。 任何帮助都非常受欢迎......

2 个答案:

答案 0 :(得分:3)

尝试这种方式:

InputStream input = httpConn.openInputStream();
byte[] xmlBytes = new byte[256];
int len = 0;
int size = 0;
StringBuffer raw = new StringBuffer();
while (-1 != (len = input.read(xmlBytes))) 
{
    raw.append(new String(xmlBytes, 0, len));
    size += len;
}
value = raw.toString();
byte[] dataArray = value.getBytes(); 
EncodedImage bitmap;
bitmap = EncodedImage.createEncodedImage(dataArray, 0,dataArray.length);
final Bitmap googleImage = bitmap.getBitmap();

答案 1 :(得分:2)

Swati的回答很好。使用更少的代码行可以完成同样的事情:

InputStream input = httpConn.openInputStream();
byte[] dataArray = net.rim.device.api.io.IOUtilities.streamToBytes(input);
Bitmap googleImage = Bitmap.createBitmapFromBytes(dataArray, 0, -1, 1);