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

Reply
Developer
pic_az
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.

 

 

Please use plain text.
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);

 

Please use plain text.
Developer
pic_az
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!
Please use plain text.