Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 36
Registered: ‎06-29-2009
My Device: 9000
Accepted Solution

InputStream and Contact.Photo

I am using the 4.6.0 9000 simulator. Here is the code I am using:

 

static final class GetPhoto extends Thread { public GetPhoto(){} public void run(){ StreamConnection s; String person = "http://somewebsite"; try{ s = (StreamConnection)Connector.open(person + (String)cvals.get("CNUM"), Connector.READ, true); HttpConnection httpConn = (HttpConnection)s; int status = httpConn.getResponseCode(); if (status == HttpConnection.HTTP_OK){ InputStream input = s.openInputStream(); byte[] data2 = new byte[input.available()]; input.read(data2, 0, input.available()); byte[] photoEncoded = Base64OutputStream.encode(data2, 0, data2.length, false, false); if (c.countValues(Contact.PHOTO) > 0) { c.setBinary(Contact.PHOTO, 0, PIMItem.ATTR_NONE, photoEncoded, 0, data2.length); } else { c.addBinary(Contact.PHOTO, PIMItem.ATTR_NONE, photoEncoded, 0, data2.length); } // if record is modified, commit if (c.isModified()){ try{ c.commit(); } catch (PIMException e){ errMessage = "Error in getPhoto commit. Error:" + e.getMessage(); errCnt++; } } s.close(); } } catch (IOException e){ errMessage = "Error in getPhoto. Error:" + e.getMessage(); errCnt++; } photoCont = true; } }

 I have used this code to download photos and it works well until I came across one that appears to be larger than the norm. I tested on one that works and the raw size returned was 7965 bytes and the encoded was 10620 bytes. When I try the larger one the raw is still 7965 but fails after that. It returns only part of the photo and the other part is scrambled. 

So, is there a max that gets returned when using InputStream.available() and do I need to put this in some sort of loop to get the whole photo? If not why then is it getting scrambled. I have used Java on the desktop side to get the phot from the same web site and it works fine so I know the photo is not corrupted. I am just using different methods of retruning the photo on the desktop side. 

Thanks.

 

 

Developer
Posts: 1,474
Registered: ‎04-14-2009
My Device: Not Specified

Re: InputStream and Contact.Photo

byte[] chunk = new byte[4 * 1024]; ByteArrayOutputStream read2Buf = new ByteArrayOutputStream(); int chunkLength; while ((chunkLength = input.read(chunk)) != -1) { read2Buf.write(chunk, 0, chunkLength); } byte[] read2 = read2Buf.toByteArray();

 

 

You need to loop on InputStream.read to read the full stream (see InputStream.read contract in J2SE/J2ME Javadocs).

 

OR BB-specific:

 

byte[] read2 = IOUtilities.stream2Bytes(input);

 

Highlighted
Developer
Posts: 36
Registered: ‎06-29-2009
My Device: 9000

Re: InputStream and Contact.Photo

That did it, thanks. I marked it complete and Kudos to ya!