Hi,
I've written code that loads images from the blackberry sd card and displays thumbnails of them. In the emulator everything works fine. But on the device (8300 v4.5.0.18 Platform 2.7.0.43) the same code doesn't work. The code is below:
File Search Code:-
Code:
private void searchDirectory(Enumeration en, String path) {
if (path == null || "".equals(path)) {
path = "file://localhost/";
}
try {
while (en.hasMoreElements() && !processCompleted) {
String fileName = (String) en.nextElement();
FileConnection node = (FileConnection) Connector.open(path + fileName);
if (node.exists()) {
if (node.isDirectory()) {
searchDirectory(node.list(), node.getURL());
} else if (isImageFile(fileName)) {
Logger.error("FileSearchTask.searchDirectory " + path + fileName + " : " + node.canRead());
Bitmap bitmap = ImageUtil.createThumbnail(node.openInputStream(),
ImageInformationHolder.getFileType(fileName));
imageCallback.imageCallback(new ImageInformationHolder(node.getURL(),
fileName, bitmap));
}
}
if (node != null) {
node.close();
}
}
} catch (Exception ex) {
Logger.error("FileSearchTask.searchDirectory", ex);
}
}
Image load Code:-
Code:
public static Bitmap createThumbnail(InputStream input, String type) {
if (input == null) {
return null;
}
try {
Enumeration en = EncodedImage.getSupportedMIMETypes();
while(en.hasMoreElements()) {
Logger.error(" Supported Image Types " + (String)en.nextElement());
}
float y = MAX_HEIGHT;
EncodedImage bitmap = null;
int available = input.available();
byte[] data = new byte[available];
input.read(data, 0, available);
Logger.error(" ImageUtil.createThumbnail : LOADING BITMAP " + type);
bitmap = EncodedImage.createEncodedImage(data, 0, data.length); // not working on device tryed bitmap = EncodedImage.createEncodedImage(data, 0, data.length, type); also doesn't work
Logger.error(" ImageUtil.createThumbnail : LOADED BITMAP");
if (bitmap.getHeight() > y) {
float x = (bitmap.getHeight() / y) * 100.0f;
Float scalePer = new Float(x);
int divisor = Fixed32.toFP(100);
/**
* Change this number, < 100 makes the image bigger by percentage.
* i.e. 90 makes the image 110% normal size. By increasing, you will
* reduce the image size, e.g. 110 makes the image 90% normal size.
*/
int multiplier = Fixed32.toFP(scalePer.intValue());
int fixedX = Fixed32.toFP(1);
/** First, divide the image scale by 100. */
fixedX = Fixed32.div(fixedX, divisor);
/** Now, multiply the image scale by the multiplier. */
fixedX = Fixed32.mul(fixedX, multiplier);
int fixedY = Fixed32.toFP(1);
fixedY = Fixed32.div(fixedY, divisor);
fixedY = Fixed32.mul(fixedY, multiplier);
Logger.error(" ImageUtil.createThumbnail : SCALING BITMAP x=" + fixedX + " y=" + fixedY );
bitmap = bitmap.scaleImage32(fixedX, fixedY);
Logger.error(" ImageUtil.createThumbnail : SCALED BITMAP");
}
input.close();
return bitmap.getBitmap();
} catch (Exception e) {
Logger.error("ImageUtil.createThumbnail", e);
return null;
}
}
I get the following errors:-
[ERROR] 9/10/2008 15:10 : FileSearchTask.searchDirectory file://localhost/SDCard/BlackBerry/pictures/IMG00006.jpg : true
[ERROR] 9/10/2008 15:10 : Supported Image Types image/x-rpi
[ERROR] 9/10/2008 15:10 : Supported Image Types image/jpg
[ERROR] 9/10/2008 15:10 : Supported Image Types image/jpeg
[ERROR] 9/10/2008 15:10 : Supported Image Types image/vnd.wap.wbmp
[ERROR] 9/10/2008 15:10 : Supported Image Types image/x-rwi
[ERROR] 9/10/2008 15:10 : Supported Image Types image/bmp
[ERROR] 9/10/2008 15:10 : Supported Image Types image/vnd.rim.png
[ERROR] 9/10/2008 15:10 : Supported Image Types image/gif
[ERROR] 9/10/2008 15:10 : Supported Image Types image/tiff
[ERROR] 9/10/2008 15:10 : Supported Image Types image/pjpeg
[ERROR] 9/10/2008 15:10 : Supported Image Types image/png
[ERROR] 9/10/2008 15:10 : Supported Image Types image/x-rdi
[ERROR] 9/10/2008 15:10 : Supported Image Types image/x-rgi
[ERROR] 9/10/2008 15:10 : ImageUtil.createThumbnail : LOADING BITMAP image/jpg
[ERROR] 9/10/2008 15:10 : ImageUtil.createThumbnail : java.lang.IllegalArgumentException: null
[ERROR] 9/10/2008 15:10 : FileSearchTask.searchDirectory file://localhost/store/samples/contacts/BlueDress.png : true
[ERROR] 9/10/2008 15:10 : FileSearchTask.searchDirectory : net.rim.device.api.system.ControlledAccessExceptio n: null
Any help is greatly appreciated.