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
New Developer
New Developer
Posts: 11
Registered: ‎11-23-2009
My Device: Not Specified

Need help in optimizing code - InputStream

Hi all,

 

In my application I need to download images from a http URL. The number of images varies depending on the user logged in. I am using the following piece of code:

 

StreamConnection streamC = (StreamConnection) 
Connector.open(http:URL);
InputStream in = streamC.openInputStream();
// Extract the data from the input stream.
byte[] data = new byte[256];
DataBuffer db = new DataBuffer();
int chunk = 0;
while (-1 != (chunk = in.read(data))) {
	db.write(data, 0, chunk);
}
in.close();
// Here is ur image in byte format.
data = db.getArray();
// image= Bitmap.createBitmapFromPNG(data, 0, -1);
EncodedImage eImage =
 EncodedImage.createEncodedImage(data, 0, data.length);

 This is done in a separate thread. The images are loaded in a for loop. The problem is that it takes longer time to load(i.e around 1 min for 8 image) and the needs to download a maximum of 22 images.

 

Could anyone suggest me how to modify this code so that the delay is reduced.

 

Thanks in advance,

baj

New Developer
Posts: 119
Registered: ‎10-23-2009
My Device: Not Specified

Re: Need help in optimizing code - InputStream

Sr! How to display images on screen? I mean all of images are displayed on a screen or not?
Developer
Posts: 467
Registered: ‎07-16-2009
My Device: Z10 LE
My Carrier: Vodafone NL

Re: Need help in optimizing code - InputStream

As I remeber the haviest in time part is establishing http connection. So to reduce it you can pack images into zip and download it by one seesion. Or you can create more than one thread for downloading them simultaneously.

Developer
Posts: 42
Registered: ‎10-14-2009
My Device: Not Specified

Re: Need help in optimizing code - InputStream

 

Seems two operations may affect on the time consumption of your app:

1. The download process

2. The decode process

 

For 1, if the speed of downloading can not be tolerated. I suggest you to download all the image files previously and select the corresponding one in the runtime.

 

For 2, you may need to do the image decoding in a seperate thread.

 

 

New Developer
New Developer
Posts: 11
Registered: ‎11-23-2009
My Device: Not Specified

Re: Need help in optimizing code - InputStream

Thanks to both of you for your suggestion. I'll try it out.

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Need help in optimizing code - InputStream

Also, increase your "chunk" size. 4096 would be a better size.

 

 

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Need help in optimizing code - InputStream

somewhere areound 4.3 or 4.5 RIM added some utility to fully read an input stream into a buffer.

Presumably this is optimized and will block when there is nothing there etc.

Also, longer isn't always better unless you have a retry strategy that works with pieces, like byte range requests

and even be careful here if downloading dynamic files ( if-modified-since etc).

 

New Developer
New Developer
Posts: 11
Registered: ‎11-23-2009
My Device: Not Specified

Re: Need help in optimizing code - InputStream

The kind of application I am developing is a social networking app  in which the images loaded are dynamic and depend on the current number of friends an user has.  Also the url of the images are retrieved at runtime  via a webservice call to the server.

 

I also tried(if this is what you meant by fully read an input stream into a buffer)

              db.write(in)  //where db is the Databuffer and in is the InputStream.

But I'm confused how to use the createEncodedImage method as the method needs a byte array.

Thanks,
baj

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: Need help in optimizing code - InputStream

I think it is called IOUtilities. Certainly if people are using stock avatars you may be able to save a lot of redundant transfers with your own local cache. Generally compression should know about the statistics of the input data- gzip on an image or or a file that is already zipped may not accomplish much.