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
trivedirujuta
Posts: 528
Registered: ‎10-06-2009
My Device: 9800,BB Dev Alpha,z10 limited edition
My Carrier: Vodafone

Image file is not opening after creating(seems to be wrongly encoded)

Hi ,

 

I have written a code that converts bitmap to JPEG and saves in a file. The file is created properly but when I try to open it, it doesn't open either in BB device or in Computer. It says image format not supported or image is not available.

 

Here is my code

JPEGEncodedImage jpg=JPEGEncodedImage.encode(bitmap, 100);
DataOutputStream out=null;
FileConnection fconn=null;
try {
fconn = (FileConnection)Connector.open("file:///store/home/user/abc.jpg");
if(!fconn.exists()){
fconn.create();
}
out = fconn.openDataOutputStream();
out.write(jpg.getData());

out.flush();
out.close();

} catch (IOException e) {

// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
//write the information in the file
try {
if (null != out)
out.close();
if (null != fconn)
fconn.close();
} catch (IOException e1) {

System.out.println(e1+"ioooooo");
}
}

 

Can somebody help me?

 

Thanks

Rujuta Trivedi
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: Image file is not opening after creating(seems to be wrongly encoded)

Note sure if this is the problem, but instead of opening a DataOutputStream, you should actually just open an OutputStream as shown:

 

JPEGEncodedImage jpg=JPEGEncodedImage.encode(bitmap, 100);
OutputStream out=null; // changed
FileConnection fconn=null;
try {
fconn = (FileConnection)Connector.open("file:///store/home/user/abc.jpg");
if(!fconn.exists()){
fconn.create();
}
out = fconn.openOutputStream(); // changed
out.write(jpg.getData());
...

Please use plain text.