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
New Developer
Posts: 3
Registered: ‎10-06-2008
My Device: Not Specified
Accepted Solution

Unable to load image

[ Edited ]

Following the example on the document "BlackBerry_Application_Developer_Guide_Volume_1", I try to reproduce the example in working with images. But always get the exception that the "Class not found" when I try to get the stream with

 

 

input = Class.forName("imagetest").getResourceAsStream("0.png");

 


 

 

the file structure is:


 

C:\Users\Gabriel\Documents\Development\BlackBerry\imagetest\

                                                                                              \0.png

                                                                                              \imageTestMain.java 

                                                                                              \test.jdw  

 

I already add the 0.png to the project.

 

And the code is:

 

package imagetest;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import java.io.InputStream;
import java.io.IOException;

public class imageTestMain extends UiApplication
{
public static void main(String[] args)
{
//create a new instance of the application
//and start the application on the event thread
imageTestMain theApp = new imageTestMain();
theApp.enterEventDispatcher();
}

public imageTestMain()
{
//display a new screen
pushScreen(new imageTestMainScreen());
}
}

//create a new screen that extends MainScreen, which provides
//default standard behavior for BlackBerry applications
class imageTestMainScreen extends MainScreen
{
private InputStream input;
private static final int IMAGE_SIZE = 2430;
private byte[] data = new byte[IMAGE_SIZE];

public imageTestMainScreen() {
//invoke the MainScreen constructor
super();
// Load Images
try {
input = Class.forName("imagetest").getResourceAsStream("0.png");
} catch (ClassNotFoundException e) {
System.out.println("Class not found");
}
if(input == null) {
System.out.println("Error: input stream is not initialized.");
} else if (input != null) {
System.out.println("OK: input stream is initialized.");
try {
int code = input.read(data);
System.out.println("Total number of bytes read into buffer: " + code + " .");
} catch (IOException e) {
// Handle exception.
}
try {
EncodedImage image = EncodedImage.createEncodedImage(data, 0, data.length);
add(new BitmapField(image.getBitmap()));
} catch (IllegalArgumentException iae) {
System.out.println("Image format not recognized.");
}
}

//add a title to the screen
LabelField title = new LabelField("ImageTest",
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
setTitle(title);

}
}

 

I appreciate any help

 

Gabo

Message Edited by elgabo on 10-06-2008 10:40 PM
Message Edited by elgabo on 10-06-2008 10:41 PM
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to load image

[ Edited ]

Three points, all of which might resolve your problem.

 

1) I'm not familiar with the example from the Guide, however I would encourage you to review the following KB article:

 

How To - Add plain text or binary files to an application
Article Number: DB-00148

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/How_To_...

 

In this article, you will see that the class name specified in the Class.forName() is fully qualified.

 

2) Rather than using the hardcoded name and using Class.forName(), you could replace this with a

(this.getClass())

Then your code would look like: 

input = (this.getClass()).getResourceAsStream( "/0.png" );

 

 

3) Finally, you seem to be going to a lot of trouble to get a Bitmap that you can use in a BitmapField.  How about the following:

 

Bitmap b = Bitmap.getBitmapResource( "0.png" );

add(new BitmapField(b);

 

If this does not find the file, then b will be null.

 

Editted to add the "/" before the filename ( "0.png" ) that is required and was missed in the original post - thanks to following poster for pointing this out. 

Message Edited by peter_strange on 10-07-2008 06:37 PM
New Developer
Posts: 3
Registered: ‎10-06-2008
My Device: Not Specified

Re: Unable to load image

Thanks peter to point me to the right direction, actually the this.getClass() help me to find the problem, but it need to be the string to the class name so I used

 

input = Class.forName(this.getClass().getName()).getResourceAsStream("/0.png");

 

 Note that this.getClass().getName() return "imagetest.imageTestMainScreen" and the inputStream input still null after that, but the documentation said:

"However, if the current project's JDP file does not reside in its assumed location, the above behaviour will prevent the resource from being found. In this case, the user should prepend a forward slash (/) to the resource name before calling getResourceAsStream."

 This why I add the / before the name of the image, and this work, the image was displayed.

 

Now for your second suggestion, I don't know why the documentation show the above example with is so easy in this way,I try it and works wonderful without all the troubles with my previous way, in conclusion I will use this second form. Thanks.

 

Do you know why the documentation suggest the first form instead the second easy one? Are there any differences in memory managements, speed, etc?

 

Gabo

 

 

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Unable to load image

[ Edited ]

Thanks for pointing out my mistake re the "/", I've updated my post to add it in.

 

I wonder if

input = Class.forName(this.getClass().getName()).getResourceAsStream("/0.png" );

is  just the same as:

input = (this.getClass()).getResourceAsStream("/0.png" );

 

Did you try this form?

 

Finally with respect to why the documentation suggested the first option (i.e. reading the file yourself) when the second firm is easier, I can only suggest that the example was written a while ago before 4.0 was released and has not been updated. 

Message Edited by peter_strange on 10-07-2008 06:36 PM
New Developer
Posts: 3
Registered: ‎10-06-2008
My Device: Not Specified

Re: Unable to load image

Yes you are correct, my mistake. I put the this.getClass() inside the Class.forName(this.getClass()) and gave me errors at compiler time, so I add the getName() to correct.

 

Finally this get only for reference because I'm using now the second form and work wonderful.

 

Regards

Gabo

Developer
Posts: 31
Registered: ‎01-31-2009
My Device: Not Specified

Re: Unable to load image

Hi Peter,

 

Is there a way to read a photograph file directly from  blackberry device.  

I have 8703e ver 4.1.0 that can not be upgraded to 4.2 or above.

 

The JVM here does not have javax.microedition.io.File that has needed interfaces and implementation with native io.

Unfortunately, they version 4.2 and above supports it.

 

The solution you presented here works fine.  But, I need to read image files from camera or user's SDCard.

 

Do you have some idea?

 

Thinakar "TK" 

Developer
Posts: 31
Registered: ‎01-31-2009
My Device: Not Specified

JPG doesn't load with: Bitmap img = Bitmap.getBitmapResource("img2.jpg");

Why is that jpg files does not get into image with getBitmapResource call as:

                Bitmap img = Bitmap.getBitmapResource("img2.jpg"); 

Blackberry supports jpg format I think.

I can load a png file of 3k size.  The jpg file is about 9k.

 

Thanks.

 

BlackBerry Development Advisor
Posts: 15,784
Registered: ‎07-09-2008
My Device: BlackBerry PRIV
My Carrier: Bell

Re: JPG doesn't load with: Bitmap img = Bitmap.getBitmapResource("img2.jpg");

[ Edited ]

The BlackBerry 8703e does have a micro SD card, so there is no way to access it.  The FileConnection APIs were added in BlackBerry handheld software version 4.2.0.

Message Edited by MSohm on 02-09-2009 02:32 PM
Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker
New Developer
Posts: 20
Registered: ‎07-21-2008
My Device: Not Specified

Re: JPG doesn't load with: Bitmap img = Bitmap.getBitmapResource("img2.jpg");

A jpg isn't a Bitmap in this context, it's an EncodedImage.

Try:

 

Bitmap img = EncodedImage.getEncodedImageResource("img2.jpg").getBitmap;

Highlighted
Developer
Posts: 35
Registered: ‎10-11-2008
My Device: Not Specified

Re: Unable to load image

Dear Peter,

 

I work with JDE 4.5.0.7 developing for Curve 8900

 

I found the solutions on this threads very useful, but I want to add that in order to load ".jpg" images it is necesary to know that the build process converts the images to ".png".

 

So, if the image file name included inside the project is "photo.jpg", the name used inside the program must be "photo.png".

 

The change in the filename/type can be avoided going to project´s properties,  compile tab,  and check "Don´t convert image files to png".

Then the filename used inside the program can be "photo.jpg".

 

 

 Thank you,