07-27-2010 03:30 PM
Hi there. This is my first post.
I'm trying to display a tiff (tagged image file format) in my app. Here's a snippet of code:
protected void paint(Graphics graphics) {
final int imageWidth = Graphics.getScreenWidth();
final int imageHeight = Graphics.getScreenHeight();
final EncodedImage image = EncodedImage.getEncodedImageResource("A.tif");
graphics.drawImage(0, 0, imageWidth, imageHeight, image, 0, 0, 0);
}
However, when i run the above i get an IllegalArgumentException during drawImage. I was also unable to open this tiff file from the native image viewer app on the BB. Has anyone successfully written code to display a tiff image file? If so, can you please help.
Compiling and running with BB JDE 4.5.0 API on BB 8300 simulator.
Cheers.
Solved! Go to Solution.
07-27-2010 03:57 PM - edited 07-27-2010 07:39 PM
I am about 99% sure tiff is not a supported format on any BlackBerry device at any OS Level.
So I think you will need to convert it to one of the supported formats. If you are adding the image to the project, this should be painless, I think most paint programs will accept tiff and convert it. But you probably need the tiff format for some specific reason. So can you explain why you need these images to be tiff?
Edit: I was wrong - see later.
07-27-2010 04:14 PM
I'm grabbing them from a remote server as tiffs. So, unfortunately i cannot make the conversion (unless it's possible to make the conversion on the BB?).
07-27-2010 04:27 PM
@peter TIFF has been supported since 4.2, it does work with most TIFFs.
Is there a message with the exception?
07-27-2010 04:47 PM
There is no message. However, if you're privy to the code, then the exception is thrown at:
Graphics.drawImage(int, int, int, EncodedImage, int, int, int) line: 1325
Also, i'm unable to open tiff files using the native picture viewing app.
07-27-2010 05:05 PM
You say you can't open the TIFF. Do you mean you can't "find" it because it is a resource in a COD file or it is a file on the SDCard/Device memory and it won't open?
07-27-2010 05:13 PM
I tried two things:
1) Added tiff as resource and tried to display using code snippet above. This failed (ie: throwing IllegalArgumentException in Graphics.drawImage line: 1325)
2) Copied tiff to sdcard and tried to open using native picture viewer. This also failed.
07-27-2010 05:42 PM
If #1 is not null you can try to get a Bitmap out of the EncodedImage to see if that would work. If you get an error there then that is a deal breaker. Can't really do much when the lowest possible image format (Bitmap) doesn't work.
07-27-2010 05:45 PM
rcmaniac25 is right, tiff are supported. My mistake.
Having said that, I don't thin they are supported as encoded into the cod file. Have a look at this from the API:
public static EncodedImage getEncodedImageResource(String module,
String name)Creates an encoded image from provided named resource found in module.
This method looks for the resource in both the .cod file that called this method and any .cod files that it relies on. The resource files are automatically generated by the compiler when a new graphic (PNG, GIF, or, JPG) is added to a project.
So you I suepect you will have to read the file into a byte array amd then use
public static EncodedImage createEncodedImage(byte[] data,
int offset,
int length,
String mimeType)Creates an EncodedImage from bytes.
A new instance of the appropriate EncodedImage subclass is returned. If the image format does not match the MIME type, or the image data is otherwise invalid an IllegalArgumentException is thrown. If the MIME type is null then the system will attempt to infer the image type from the image data.
Parameters:
data - The byte array containing the encoded image data. (NOT base64 encoded)
offset - The starting offset of the encoded image data in the array.
length - The length in bytes of the encoded image data, or -1 to indicate the entire array should be used.
mimeType - The MIME type of the image data.
The following MIME types are supported:
•"image/gif"
•"image/png"
•"image/vnd.wap.wbmp"
•"image/jpeg" (supported only on Colour devices)
•"image/jpg" (supported only on Colour devices)
•"image/pjpeg" (supported only on Colour devices)
•"image/bmp"
•"image/tiff"
07-27-2010 05:55 PM
That could possibly be it but that doesn't explain why the native picture viewer can't show them when they are files on SDCard/Device memory.
Also if rapc doesn't see tiff as a image format it means that it wouldn't convert it to png, so they could open a InputStream to it and read in all the data, if the images are coming from a server then this makes it easier to change where the InputStream comes from and still have it work.