BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-23-2009, 01:27 PM   #1 (permalink)
New Member
 
Join Date: Jun 2009
Model: 7100T
PIN: N/A
Carrier: hi
Posts: 6
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with sending image to blackberry

Please Login to Remove!

Hi,

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:
Code:
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:

Quote:
SocketConnection sc = (SocketConnection) Connector.open("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.

Thanks,
Sean
Offline  
Old 07-23-2009, 08:56 PM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

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.

Last edited by Dougsg38p : 07-23-2009 at 08:57 PM. Reason: Added info
Offline  
Old 07-23-2009, 10:52 PM   #3 (permalink)
New Member
 
Join Date: Jun 2009
Model: 7100T
PIN: N/A
Carrier: hi
Posts: 6
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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:
Code:
InputStream is = (this.getClass()).getResourceAsStream("/pic.png");
byte[] data = new byte[5000];
is.read(data, 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:
Code:
byte[] data = new byte[5000];
is.read(data, 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).
Offline  
Old 07-24-2009, 07:07 PM   #4 (permalink)
New Member
 
Join Date: Jun 2009
Model: 7100T
PIN: N/A
Carrier: hi
Posts: 6
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

fix't
Offline  
Old 07-27-2009, 09:16 AM   #5 (permalink)
New Member
 
Join Date: Oct 2008
Model: 8800
PIN: N/A
Carrier: rogers
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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 = input.read(data1)) != -1)
{
db.write(data1, 0, chunk);
}

input.close();
c.close();
data1 = db.getArray();
panel1 = Bitmap.createBitmapFromBytes( data1, 0, -1, 3 );
Offline  
Old 07-28-2009, 01:18 PM   #6 (permalink)
New Member
 
Join Date: Jun 2009
Model: 7100T
PIN: N/A
Carrier: hi
Posts: 6
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's my code for the server:

Code:
// byte[] data;
Socket s = ss.accept();
OutputStream os = s.getOutputStream();
PrintWriter pw = new PrintWriter(os);
pw.print(data.length);
pw.print('\n');
pw.flush();
os.write(data, 0, data.length);
os.flush();
And for the blackberry app:

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

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

// get the data
byte[] data = new byte[size];
is.read(data, 0, size);

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


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.