BlackBerry Forums Support Community

BlackBerry Forums Support Community (
-   Developer Forum (
-   -   Problem with sending image to blackberry (

capndurk 07-23-2009 01:27 PM

Problem with sending image to blackberry

I'm trying to send a small image (PNG or JPG) from my computer to my blackberry simulator via a socket. The entire image seems to transfer fine, the problem arises when I try to display it, by calling:

EncodedImage.createEncodedImage(data, 0, data.length);
// or
Bitmap.createBitmapFromBytes(data, 0, -1, 0);
// or
Bitmap.createBitmapFromPNG(data, 0, -1);

I keep getting an IllegalArgumentException error when I call any of these functions with the byte[] that I pass into the constructor of my derived MainScreen class. I don't know what else to do; I've looked up countless other forum posts and used their code to transfer, and it still doesn't work. Here's how I'm connecting to the server:


SocketConnection sc = (SocketConnection)"socket://<my computer's IP>:5000;deviceside=true");
Please let me know if you guys need any more information. The simulator I'm using is for the BB Storm, and I can copy all the code for both the server and the simulator if needed. I am desperate to find a solution for this.


Dougsg38p 07-23-2009 08:56 PM

This works for me:

m_image = Bitmap.createBitmapFromBytes(image,0,size,1);

Here is the doc:

public static Bitmap createBitmapFromBytes(byte[] bytes,
int offset,
int length,
int scale)

Looks like you are sending length = -1, you should be sending the size of the image.

Also, the scale should be 1, not 0.

capndurk 07-23-2009 10:52 PM

Thanks for the response. The scale thing is actually a typo from copying my code to the forum post, I did use scale 1. And for the length, the documentation said it can be -1 (it will read to the end of the byte array). In any case, I've tried data.length anyway.

As a small update, I have successfully been able to display the same image with the following:

InputStream is = (this.getClass()).getResourceAsStream("/pic.png");
byte[] data = new byte[5000];, 0, 5000);  // read image
Bitmap b = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);
add(new BitmapField(b));

The code gets an IllegalArgumentException when I use the following code:

byte[] data = new byte[5000];, 0, 5000);
Bitmap b = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);
add(new BitmapField(b));

I've tried pretty much everything.. I can't figure this out. Oh and by the way, I'm getting the data on the server using a FileInputStream (Java 6 or something).

capndurk 07-24-2009 07:07 PM


toscaty 07-27-2009 09:16 AM

What did you end up doing to fix this issue? I have exactly the same problem - but I throw the exception "illegalArgumentException" only on the device and not on the simulator - I am using a Databuffer and reading from an http connection - then I get the entire image and scale it down - but it keeps throwing this exception??

DataBuffer db = new DataBuffer();
byte[] data1 = new byte[256];

int chunk = 0;
while((chunk = != -1)
db.write(data1, 0, chunk);

data1 = db.getArray();
panel1 = Bitmap.createBitmapFromBytes( data1, 0, -1, 3 );

capndurk 07-28-2009 01:18 PM

Here's my code for the server:


// byte[] data;
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);
os.write(data, 0, data.length);

And for the blackberry app:


SocketConnection sc = (SocketConnection)"socket://<ip>:5000;deviceside=true");
InputStream is = sc.openInputStream();

// get image size from server
String sizeStr = "";
while((c = (char) != '\n')
    sizeStr = sizeStr + String.valueOf(c);
int size = Integer.parseInt(sizeStr);

// get the data
byte[] data = new byte[size];, 0, size);

// create the bitmap
Bitmap b = Bitmap.createBitmapFromBytes(data, 0, data.length, 1);

All times are GMT -5. The time now is 08:46 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.