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
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to save & delete a Bitmap image in Blackberry Storm?

I am not getting actually what are you trying.

In the above code you posted, you are converting your image bmp to bytearray ( bitmapBuffer). And in the next line you are trying to covering is back to bitmap. And also you are trying to get Encoded image from data. Where is data comes from.

 

Are you trying save your image *.bmp in the photo directory named as picIndex.bmp.

If Yes then:

For saveing image:

1. create a file connection using file path

2. Create a OutputStream using that  path

3. Convert your image to byte array

4. Write the data from bytearray to outputstream.

5. You are done.

For viewing image

Please have  a look at the post that I potsed two post before this.

If Not then:

Its better if you can explain your problem clearly.

 

Also have a look at the following thread. Here Bitmap to ByteArray conversion explained clearly.

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=736&query.id=1196...

 

Regards

Bikas

 

 

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

Re: How to save & delete a Bitmap image in Blackberry Storm?

I m not reading but writing from Bitmap.

 

in getBytesFromBitmap(Bitmap bmp) I m wring "dos.write()" . So i m using ByteArrayOutputStream.

Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: How to save & delete a Bitmap image in Blackberry Storm?

You have to read in the saved image first using an InputStream before you can write the image out. You are using a ByteArrayOutputStream to read in your Bitmap which is likely where the IllegalArgument comes in.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to save & delete a Bitmap image in Blackberry Storm?

[ Edited ]

Yes. i m saving image *.bmp in the Media -> Pictures -> All Pictures as picIndex.bmp.

 then:

For saveing image:

1. create a file connection using file path

2. Create a OutputStream using that  path

3. Convert your image to byte array

4. Write the data from bytearray to outputstream.

5. You are done.

 

I m doing all this like...


private void saveBitmap(int picIndex, Bitmap bmp)
{
try
{
FileConnection fconn = (FileConnection)Connector.open(PHOTO_DIR + picIndex + EXTENSION, Connector.READ_WRITE);

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

OutputStream outputStream = fconn.openOutputStream();
byte[] bitmapBuffer = getBytesFromBitmap(bmp);
//Bitmap bitmapImage = Bitmap.createBitmapFromBytes(bitmapBuffer,0,bitmapBuffer.length,1);
//EncodedImage encodedImage = EncodedImage.createEncodedImage(bitmapBuffer,0,bitmapBuffer.length);
//Bitmap bitmapImage = encodedImage.getBitmap();

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

 


 I m able to save the Bitmap in Simulator in Media -> Pictures -> All Pictures as picIndex.bmp but cant view it after opening.

 

When I tries to open it error message comes on console as


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

 


I just want to view it in Simulator & not in MainScreen of my app. So I havent wrote ur code for viewing in MainScreen.

 

So in saveBitmap(...) I m writing


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

or

EncodedImage encodedImage = EncodedImage.createEncodedImage(bitmapBuffer,0,bitmapBuffer.length);
Bitmap bitmapImage = encodedImage.getBitmap();
 

 

for viewing purpose but after adding this line IllegalArgumentException is coming.


is my method getBytesFromBitmap(Bitmap bmp) posted above is correct?

If its wrong then How to get byte[] from a Bitmap? 
Message Edited by shreyasb on 08-31-2009 08:49 AM
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to save & delete a Bitmap image in Blackberry Storm?

[ Edited ]

Hi CMY,

 

u mean to say I have to read using InputStream in "getBytesFromBitmap(Bitmap bmp)" instead of "dos.writeInt(rgbdata[i])"  

 

Then How to get byte[] from a Bitmap? 

 

plz give me a code snippet for  "getBytesFromBitmap(Bitmap bmp)"...

Message Edited by shreyasb on 08-31-2009 07:27 AM
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to save & delete a Bitmap image in Blackberry Storm?

The problem is in your bitmap to bytearray conversion.

 

For coverting bitmap to byte array you can use the PNGEncoder class posted by richard_puckett.

You will get the class Here.

Reference link:

http://supportforums.blackberry.com/rim/board/message?board.id=java_dev&message.id=736&query.id=1196...

 

So I commented out your conversion method and used the above class. Then it worked. Like:

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

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

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

OutputStream outputStream = fconn.openOutputStream();

// bitmap to byte array conversion
//byte[] bitmapBuffer = getBytesFromBitmap(bmp);
PNGEncoder encoder = new PNGEncoder(bmp, true);
byte[] bitmapBuffer = encoder.encode(true);

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

 

 

Regards

Bikas

 

 

 

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

Re: How to save & delete a Bitmap image in Blackberry Storm?

[ Edited ]

Thanks Bikas.

 

There was problem in bitmap to byte[] conversion.

 

by adding ur code or

 

 

PNGEncoder encoder = new PNGEncoder(bitmap, true);
byte[] imageBytes = encoder.encode(true);

OR

PNGEncodedImage encodedImage = PNGEncodedImage.encode(bitmap); //PNGEncodedImage class is RIM-defined.

byte[] imageBytes = encodedImage.getData();

 

 

 

, the bimap is saved in Media->Pictures->Pictures Folder

 

but when clicked on it to view.

 

 

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

 

 I m testing on Storm 9500 Simulator. IS it Simulator problem? I will test it on Storm 9500 device. If it worked on device then its great..
Message Edited by shreyasb on 09-01-2009 04:00 AM
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: How to save & delete a Bitmap image in Blackberry Storm?

I tested it on 8800 simulator. It worked fine.

 

Regards

Bikas

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

Re: How to save & delete a Bitmap image in Blackberry Storm?

I m working in Blackberry JDE 4.7.0.41 & tested in Blackberry 9500 & 9530 Simulators but its not working. The same above problem is coming. Not getting what may be the problem.  Plz suggest.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

Re: How to save & delete a Bitmap image in Blackberry Storm?

What I m thinking is after..

 

 

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); byte[] imageBytes = encoder.encode(true); outputStream.write(imageBytes); System.out.println(" outputStream.write: ./......."); outputStream.close(); fconn.close(); } catch(Exception e){ System.out.println(" Exception while saving Bitmap:: "+e.toString()); } }

 

 The bitmap is saved but cannot be viewed.

 

 Now in this I m saving the image with EXTENSION = ".bmp" (FileConnection is opened for it) & after

 

PNGEncoder encoder = new PNGEncoder(bitmap, true);

 

 that bitmap is converted to .png file. I m getting the byte[] from that file & saving it as "1.bmp" after wring it to outputStrem.

 

But I tried with EXTENSION = ".png" then also its not working. Its getting saved but same error "Unable t ocreate Encoded image for ....." error. while viewing it.

 

What may be the problem?