Display a static image in Google Map in BlackBerry 5.0

I have a really interesting problem:

I get a static google map image with a url like this .

I tried several ways to get this information: Getting the "remote resource" as ByteArrayOutputStream, saving the image in the SD simulator, etc. ... but every freaking time I get an IlegalArgumentException .

I always get a 200 http response and the correct MIME type ("image / png"), but in any case: extracting the image and converting it to a bitmap image or saving the image to SD and reading it later; I get the same result ... the file is always corrupted.

I really believe in his coding problem or reading method (similar to this):

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 imageData.lenght (the answer is content length: 6005), but I really can't figure it out. Any help is more than welcome ...

+3
source share
2 answers

try as follows:

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();
+3
source

Swati's answer is good. You can do the same with a lot of 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);
+2
source

Source: https://habr.com/ru/post/1787320/


All Articles