Farid Farhat
Farid Farhat

Reputation: 2310

Image Browser in blackberry

I want to make an image browser for my application where the user can select the image that he wants from the phone. I am reading each image from the phone like this:

FileConnection fileConnection = (FileConnection)Connector.open((String) imm.elementAt(i));
InputStream inputStream = fileConnection.openInputStream();
byte[] imageBytes = new byte[(int)fileConnection.fileSize()];
inputStream.read(imageBytes);
inputStream.close();
fileConnection.close();

EncodedImage image = EncodedImage.createEncodedImage(imageBytes, 0, -1);
EncodedImage image1 = scaleImageEncoded.scaleImage(image, (int) (Display.getWidth() / 4) - 10, (int) (Display.getWidth() / 4) - 10);
BrowseBitmapField field = (BrowseBitmapField) manager.getField(i);
field.setBitmap(image1.getBitmap());
field.setEncodedImage(image);

When the phone included many images, this process bicomes too slow and the phone needs a lot of time to get them. Is there any faster way to read an image from the memory of the phone and display it?

Thanks in advance

Upvotes: 2

Views: 966

Answers (3)

Vit Khudenko
Vit Khudenko

Reputation: 28418

In addition to what jprofitt recommends I'd suggest to do the following:

  1. When you create a resized version of an image you can save it for future usages. Next time user clicks on the same image you first check for a resized image presence and if not present only then read/resize the original image. So you can save time spent on future resizing + don't forget that resized image file size is in times smaller than original, so future reading will happen in times faster.

  2. Do not try to read/resize all images in the dir at once. Do this only for those that should be visible for the user at the moment. For instance, if the the dir has 100 images you'll most likely need A LOT of time to read/resize/attach to manager all images. However user sees just 6 or 8 images on the screen at a time. If user scrolls up/down, then it's right time to request reading/resizing/etc for those images that now should be visible, but have not been read/resized yet.

  3. Reading/resizing should be done on a background Thread in order not to block the main UI-thread. In other words user should be able to scroll the screen while reading/resizing happens on a background Thread. I recommend to arrange a queue of reading/resizing tasks and execute them on a background worker Thread. When a task is done on worker Thread and it's time to update UI of the screen with resized image, then use UiApplication.invokeLater(Runnable action) for that.

Upvotes: 3

Ray Vahey
Ray Vahey

Reputation: 3065

The guy who posted at the below URL figured out you can get the thumbnail from BBThumbs.dat and posted some code. I haven't tried it myself but if it works the time savings should be very significant.

http://supportforums.blackberry.com/t5/Java-Development/Thumbnails-work-around/td-p/343870

Upvotes: 1

jprofitt
jprofitt

Reputation: 10964

You may try using a BufferedInputStream. I've had luck with it speeding up my read times, might work for you as well.

 BufferedInputStream inputStream = new BufferedInputStream(fileConnection.openInputStream());
 byte[] imageBytes = new byte[(int)fileConnection.fileSize()];
 inputStream.read(imageBytes, 0, imageBytes.length);

Upvotes: 2

Related Questions