01-28-2010 05:58 AM
Hi all, am trying to create a bitmap image using EncodedImage.createEncodedImage(, , ,);
but it is throwing illegal argument exception... meaning that the byte data it is taking is of unknown format, but am collecting this data from a .png file, on the SDCard of the device.
can anyone check this code and tell where am wrong?
public void addImage(String _filePath)
{
ByteArrayInputStream byteInputStream;
InputStream iStream = null;
FileConnection conn = null;
String URI = "file:///"+_filePath;
// byte []byteStream;
// opening connection to the selected location
try
{
add(new LabelField("befor opening conn"));
conn = (FileConnection)Connector.open(URI);
if(conn != null)
add(new LabelField("after opening conn, conn estblshd"));
iStream = conn.openDataInputStream();
// String str = iStream.toString();
// byteStream = str.getBytes();
int bytesRead=0;
int bytesToRead=1024;
byte[] input = new byte[bytesToRead];
if( iStream != null)
{
add(new LabelField("before while, and iStream != null"));
while (bytesRead < bytesToRead)
{
int result = iStream.read(input, bytesRead, bytesToRead - bytesRead);
if (result == -1) break;
bytesRead += result;
}
}
if( input != null)
{
add(new LabelField("input != null"));
EncodedImage eiImage = EncodedImage.createEncodedImage(input, 0, input.length); /// error here....
if( eiImage != null)
{
add(new LabelField("eiImage != null"));
Bitmap bmp = eiImage.getBitmap();
BitmapField bmpField = new BitmapField(bmp);
if( bmpField != null)
this.add(bmpField);
else
this.add(new LabelField("bmp is null"));
}
}
updateLayout();
}
catch (IOException e)
{
System.out.println("IOException:"+e.getMessage());
add(new LabelField("IOException:"+e.getMessage()));
}
finally
{
if( conn!= null)
try {
conn.close();
} catch (IOException e) {
add(new LabelField("IOException:"+e.getMessage()));
e.printStackTrace();
}
if(iStream != null)
try {
iStream.close();
} catch (IOException e) {
add(new LabelField("IOException:"+e.getMessage()));
e.printStackTrace();
}
}
i found from debugging, that input stream is creating, but the error is throwing while the creation of the EncodedImage.
plzzzz help.....
Solved! Go to Solution.
01-28-2010 06:28 AM
My response on this Thread applies here too:
Until you are 100% positive that the data you have on the BlackBerry is the same as the data you have on the Server, there is no point looking anyone any further at this problem.
01-28-2010 06:31 AM
hi, just to update... i tried with the following piece of code also, but the same result..... someone helppppp
Bitmap bmp = Bitmap.createBitmapFromBytes(input, 0, -1,1);
01-28-2010 06:34 AM
Why do you have the bytesToRead hard-coded? You are only reading 1K of data no matter how large your PNG is which is probably why the file data is incorrect. Why not get the bytes to read from the filesize?
01-28-2010 06:35 AM
the data is not from teh server, i copied a .png file from my PC to the SDCard. in my app, the file is recognizing as .png even. so there might be no chance for data corruption....
01-28-2010 07:09 AM
"i copied a .png file from my PC to the SDCard"
Apologies, missed that.
CMY didn't and I think he has spotted your bug.
01-28-2010 07:24 AM
but what is the solution!!!!???? I tested in teh device also, it is throwing the same exception.
Originally my task is to browse an image, from the device and display it on the screen. For this i used FilePickerDemo to browse for the files, and extended my functionality of creating a bitmap from the file path picked from the file browser.
Is there any other alternative to achieve this? or kindly suggest me how to fix this?
01-28-2010 08:24 AM - edited 01-28-2010 08:25 AM
Try replacing
bytesToRead = 1024;
with
bytesToRead = conn.fileSize();
01-28-2010 08:43 AM
Excellent!!! working like a charm.... Thanks a lot !!!!