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
Accepted Solution

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

[ Edited ]

I have a ready Bitmap image. Now I want to save & delete it. I m doing it like..

 

 

FileConnection fconn = (FileConnection)Connector.open("file:///store/home/user/StoredBitmap/"+picIndex+".bmp",Connector.READ_WRITE);

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

OutputStream out = fconn.openOutputStream();

if(image == null)
System.out.println(" image null ");
else
{
out.write(byte[]);
out.close();
}

fconn.close();

 

For Blackberry Storm device & simulator, which path to give instead of 

 

"file:///store/home/user/StoredBitmap/"+picIndex+".bmp"

 

 

 

I have a created Bitmap.  In outputStream, how to write it? 

 

I m using Blackberry 4.7 (Version: 4.7.0.41). In its simulator, how to save the Bitmap? I m doing it for Blackberry Storm.

 

& for deleting that Bitmap, can we use File class or we've to use FileConnection class?

 

 

 

 

 

Message Edited by shreyasb on 08-29-2009 07:59 AM
Message Edited by shreyasb on 08-29-2009 08:13 AM
Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

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

Save Image

-----------------

 

For saving image from byte array your above example should work.

 

To save the image in the device default photo directory use filepath like:

private static final String PHOTO_DIR = System.getProperty("fileconn.dir.photos"); private static final String EXTENSION = ".bmp"; private String imageName = "myImage". FileConnection file = (FileConnection)Connector.open(PHOTO_DIR + imageName + EXTENSION );

 

 

For Delete File

----------------------

 

FileConnection fc = (FileConnection)Connector.open(filePath); fc.delete();

 

Regards

Bikas

 

 

Please use plain text.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

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

[ Edited ]

 

For saving image from byte array this code is ok. But can we save a alreadey created image as I alreday have a ready mage .

 

As for saving purpose I have to create byte[] from image & again convert image from that byte[] to view. Instaed of that can we directly save image (without creating byte[])?

 

I have a image as "1.bmp". But cant save it in PHOTO_DIR.

 

I am doing like..

 

 

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

 

private void saveImage(picIndex)

{

 

ExtendedFileConnection fconn = (ExtendedFileConnection)Connector.open(PHOTO_DIR + "." + picIndex + EXTENSION, Connector.READ_WRITE);

 

 This picIndex I m getting at run-time. like "1.bmp, 2.bmp, 3.bmp....."

 

 

 How to open fconn?


 

 

 

Message Edited by shreyasb on 08-29-2009 09:46 AM
Please use plain text.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

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

[ Edited ]

How to view the saved image in Blackberry storm (device & simulator)

 

 & can we use the same PHOTO_DIR to save in Blackberry Storm device also?

 

 

private static final String PHOTO_DIR = System.getProperty("fileconn.dir.photos");

 

 

 

Message Edited by shreyasb on 08-29-2009 09:47 AM
Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

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

For saving image

--------------------------

AFAIK,  you cannot save image directly other than conveting to byte array.

 

For saving  1.bmp in photo directory use path like:

private static final String PHOTO_DIR = System.getProperty("fileconn.dir.photos"); private static final String EXTENSION = ".bmp"; //picIndex = 1 FileConnection fconn = (FileConnection)Connector.open(PHOTO_DIR + picIndex + EXTENSION, Connector.READ_WRITE);

 

 

Regards

Bikas

     

Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

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


How to view the saved image in Blackberry storm (device & simulator)

About your above question try something like:

 

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[] data = baos.toByteArray(); input.close(); fconn.close(); EncodedImage encodedImage = EncodedImage.createEncodedImage (data,0,data.length); Bitmap bitmapImage = encodedImage.getBitmap(); } else { System.out.prinntln("No Image Found"); }

And now use a BitmapField to view your image in  MainScreen. like

       BitmapField imageField = new BitmapField(bitmapImage);

       mainScreen.add(imageField);

 


& can we use the same PHOTO_DIR to save in Blackberry Storm device also?

About your above question,  I thinkYes.

 

Regards

Bikas

 

 

 

 

Please use plain text.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

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

[ Edited ]

for viewing the saved image, I m asking the path in Simulator.

 

I m saving the image 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 out = fconn.openOutputStream();
byte[] bitmapBuffer = getBytesFromBitmap(bmp);
Bitmap bitmapImage = Bitmap.createBitmapFromBytes(bitmapBuffer,0,bitmapBuffer.length,1);

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

if(bitmapImage == null)
System.out.println(" image null ");
else
{
out.write(bitmapBuffer);
out.close();
}

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


public byte[] getBytesFromBitmap(Bitmap bmp) {
try {
int height=bmp.getHeight();
int width=bmp.getWidth();
int[] rgbdata = new int[width*height];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(bos);
//Graphics g = new Graphics(bmp);
bmp.getARGB(rgbdata,0,width,0,0,width,height);

for (int i = 0; i < rgbdata.length ; i++) {
if (rgbdata[i] != -1)
{
dos.writeInt(rgbdata[i]);
dos.flush();
}
}
bos.flush();
return bos.toByteArray();
} catch (Exception ex) {
return null; }
}

 

But 

java.lang.IllegalArgumetException

 

for 

 

 

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


&

 


EncodedImage encodedImage = EncodedImage.createEncodedImage
(data,0,data.length);

 

 

Any clues why this Exception is coming?


Message Edited by shreyasb on 08-31-2009 03:29 AM
Please use plain text.
Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

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

I think you will get the image ar Media -> Pictures -> My Pictures in simulator.

 

Also have a look at this KB article to get some more information about file path.

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/What_Is...

 

Regards

Bikas

 

Please use plain text.
Developer
shreyasb
Posts: 152
Registered: ‎07-17-2009
My Device: Not Specified

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

Yes. I m getting the image at Media -> Pictures -> My Pictures in simulator

 

But because of the IllegalArgument Exception at 

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

 

I m not able to view at.

If I comment above line then the iamge is saved there but while opening mesage o/p comes on console as..

 

 

Unable to create Encoded Image for 1.bmp at file:///store/home/.....

 

How to get rid of this Exception?

 

 

 

 

Please use plain text.
Developer
Developer
CMY
Posts: 1,122
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook
My Carrier: Verizon

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

In your function to read in the Bitmap you r are using a ByteArrayOutputStream, shouldn't you be using an InputStream?
Please use plain text.