08-31-2009 04:26 AM
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.
Regards
Bikas
08-31-2009 05:21 AM
I m not reading but writing from Bitmap.
in getBytesFromBitmap(Bitmap bmp) I m wring "dos.write()" . So i m using ByteArrayOutputStream.
08-31-2009 05:37 AM
08-31-2009 06:04 AM - edited 08-31-2009 08:49 AM
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,bitmap
Buffer.length,1); or
EncodedImage encodedImage = EncodedImage.createEncodedImage(bitmapBuffer,0,bit
mapBuffer.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?
08-31-2009 06:40 AM - edited 08-31-2009 07:27 AM
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)"...
09-01-2009 02:28 AM
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:
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
09-01-2009 03:59 AM - edited 09-01-2009 04:00 AM
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);
ORPNGEncodedImage 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..
09-01-2009 04:18 AM
I tested it on 8800 simulator. It worked fine.
Regards
Bikas
09-01-2009 07:02 AM
09-01-2009 07:15 AM
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?