12-28-2009 08:58 PM
There are 2 parts to this question:
1. Can I take a BlackBerry Graphics object and convert it into PNG image data?
If not directly, then perhaps Graphics -> Bitmap -> PNG? I know Bitmap -> PNG is possible, but can't find anything on Graphics -> Bitmap.
If PNG is not an option, JPEG would be fine too.
2. Using some filesystem API, is it possible, given PNG image bytes and a file name, to write the PNG bytes to the Pictures/Photos folder that can then be accessed using the built-in photo/image viewer?
Any answers/advice for either of the steps is appreciated.
12-28-2009 09:26 PM
This questions exists for nearly every platform that ever existed. The short answer is no unless you create the Graphics object from an image.
If someone has figured out how to do this I will defiantly give Kudos because, for whatever reason, almost every other app I work on requires this. I always need to create a buffer to draw to then draw the buffer to the final destination Graphics object.
12-28-2009 09:57 PM - edited 12-28-2009 10:08 PM
I've found this thread:
there is a post that answers #2 -- saving to the Photos folder.
What remains is going from a Graphics object to a byte array of any image format readable by the built-in image viewer.
Graphics -> Bitmap might be a step in the right direction.
I've tried this:
Bitmap bitmap = new Bitmap(1000, 1000);
Graphics graphics = new Graphics(bitmap);
graphics.drawRect(5, 5, 990, 990);
PNGEncodedImage pngEncImg = PNGEncodedImage.encode(bitmap);
However, Line 2 throws an IllegalArgumentException -- because it's read-only? Any way to make it writeable?
12-28-2009 10:11 PM
The documentation says to use Graphics.create instead of new Graphics.
12-28-2009 10:28 PM
new Graphics(Bitmap) is deprecated at least starting 4.7, but I'm on 4.6 -- .create does not exist in 4.6.
I almost have this working exactly as I need as I'm able to EDIT an image and save it, but not create one using new Bitmap(width, height). In the latter case, IllegalArgumentException is thrown. Any idea why?
As a workaround, of course, I can create a blank image with the correct dimensions and edit that every time, but surely there must be another way.
12-28-2009 10:55 PM
I learn something new every day, I'm so used to Graphics objects (on BlackBerry and other platforms) having a static creation function I assumed it was around earlier then 4.7.
As for your Exception, your getting that when you create a new Bitmap? That's strange, have you tried using a different values? I never encountered that exception (unless I had a negative size). But if the code you posted is the exact same code that you are testing with then I see no reason for the exception to be thrown.