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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: Bitmap to ByteArray

[ Edited ]

Hi all,

 

I m saving the image as

 

 

private void saveBitmap(int picIndex, Bitmap bitmap)
{

String PHOTO_DIR = System.getProperty("fileconn.dir.photos");
String EXTENSION = ".bmp";

 

try
{
FileConnection fconn = (FileConnection)Connector.open(PHOTO_DIR + picIndex + EXTENSION, Connector.READ_WRITE);

if(!fconn.exists())
fconn.create();

OutputStream outputStream = fconn.openOutputStream();

PNGEncoder encoder = new PNGEncoder(bitmap, true); //PNGEncoder class created by Richard

byte[] imageBytes = encoder.encode(true);

outputStream.write(imageBytes);
outputStream.close();
fconn.close();
}
catch(Exception e){
System.out.println(" Exception while saving Bitmap:: "+e.toString());
}
}

 

 (using the PNGEncoder class created by Richard), the bimap is saved in Media->Pictures->Pictures Folder as "1.bmp".

 

but when clicked on it to view.

 

Unable to create EncodedImage for: /store/home/user/pictures/1.bmp

 error comes on console.

 

 I tried changing EXTENSION = ".png" but no use.

 

 I tried adding...


 

EncodedImage fullImage = EncodedImage.createEncodedImage(imageBytes, 0, imageBytes.length);
Bitmap bitmapImage = fullImage.getBitmap();

 

after outputStream.write(..); but no use

 

Its getting saved but same error "Unable toc reate Encoded image for: ....." error. while viewing it.

 

I m working in Blackberry JDE 4.7.0.41 & testing in Blackberry 9500 & 9530 Simulators. I tested it on Storm 9500 device but not working.

The same above problem is coming.

 

Not getting what may be the problem.  Plz suggest....

Message Edited by shreyasb on 09-01-2009 10:03 AM
Message Edited by shreyasb on 09-01-2009 10:04 AM
Highlighted
Developer
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: Bitmap to ByteArray

I m saving the image like....

 

 

private void saveBitmap(int picIndex, Bitmap bmp)
{

 

String PHOTO_DIR = System.getProperty ("fileconn.dir.photos");
String EXTENSION = ".bmp";
String filePath = PHOTO_DIR + picIndex + EXTENSION;

 

try
{
FileConnection fconn = (FileConnection)Connector.open(filePath, Connector.READ_WRITE);

if(fconn.exists())

fcomm.delete();

 

fconn.create();

OutputStream outputStream = fconn.openOutputStream();

 

PNGEncodedImage encodedImage = PNGEncodedImage.encode(bmp);
byte[] imageBytes = encodedImage.getData();
outputStream.write(imageBytes);
outputStream.close();
fconn.close();
}
catch(Exception e){
System.out.println(" Exception while saving Bitmap:: "+e.toString());
}
}

 


It is saved in Media->Pictures->Device Memory->1.bmp but when clicked on it error comes as "Unable to create Encoded Image for ..." as I told in my previous post.
I tested this in 9500 Simulator & Storm 9500.

 

Now I accessed that saved image through another project by adding that image in BitmapField & adding it on Screen like...

 

 

private void readBitmap(int picIndex)
{
String PHOTO_DIR = System.getProperty ("fileconn.dir.photos");
String EXTENSION = ".bmp";
String filePath = PHOTO_DIR + picIndex + EXTENSION;

try
{
FileConnection fconn = (FileConnection)Connector.open(filePath, Connector.READ);
if (fconn.exists())
{
InputStream input = fconn.openInputStream();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int j = 0;
while((j=input.read()) != -1) {
baos.write(j);
}
byte[] byteArray = baos.toByteArray();

Bitmap bitmapImage = Bitmap.createBitmapFromBytes(byteArray,0,byteArray.length,1);

BitmapField bitmapField = new BitmapField(bitmapImage);
hfm.add(bitmapField);
}
else
{
System.out.println("No Image Found");
}
}
catch(Exception e)
{
System.out.println(" Exception while reading Bitmap:: "+e.toString());
}

}

 

 

 

Its coming properly. I tested this in 9500 Simulator & Storm 9500.

Then dont know why its not getting displayed in Media->Pictures->Device Memory->1.bmp.

Do anyone know the answer for this?

Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: Bitmap to ByteArray

Bitmap to bytearray


Bitmap b=Bitmap.getBitmapResource("bg.png");
PNGEncodedImage image =PNGEncodedImage.encode(b);
byte[] by=image.getData();