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
Developer
shreyasb
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
Please use plain text.
Developer
shreyasb
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?

Please use plain text.
Developer
Aman_vivek
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();

 

 

 

Please use plain text.