Display static Google Map image in BlackBerry 5.0

扶醉桌前 提交于 2019-12-12 16:07:28

问题


I'm having a really interesting problem to solve:

I'm getting a static google map image, with an URL like this.

I've tried several methods to get this information: Fetching the "remote resource" as a ByteArrayOutputStream, storing the Image in the SD of the Simulator, an so on... but every freaking time I get an IlegalArgumentException.

I always get a 200 http response, and the correct MIME type ("image/png"), but either way: fetching the image and converting it to a Bitmap, or storing the image in the SD and reading it later; I get the same result... the file IS always corrupt.

I really belive its an encoding problem, or the reading method (similar to this one):

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;
}

The only thing that comes to mind is the imageData.lenght (in the response, the content length is: 6005 ), but I really can't figure this one out. Any help is more than welcome...


回答1:


try this way:

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();



回答2:


Swati's answer is good. This same thing can be accomplished with many fewer lines of code:

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


来源:https://stackoverflow.com/questions/4775409/display-static-google-map-image-in-blackberry-5-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!