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
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified
Accepted Solution

EncodedImage.createEncodedImage = IllegalArgumentException

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.....

 

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: EncodedImage.createEncodedImage = IllegalArgumentException

My response on this Thread applies here too:

 

http://supportforums.blackberry.com/t5/Java-Development/PNGEncodedImage-getBitmapImpl-gt-IllegalArgu...

 

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. 

Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: EncodedImage.createEncodedImage = IllegalArgumentException

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);

 

 

Thanks and Regards,
PraveenGoparaju.
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: EncodedImage.createEncodedImage = IllegalArgumentException

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?

Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: EncodedImage.createEncodedImage = IllegalArgumentException

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....

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: EncodedImage.createEncodedImage = IllegalArgumentException

"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.

Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: EncodedImage.createEncodedImage = IllegalArgumentException

:smileyhappy: 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?

Thanks and Regards,
PraveenGoparaju.
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: EncodedImage.createEncodedImage = IllegalArgumentException

[ Edited ]

Try replacing

   bytesToRead = 1024;

with

   bytesToRead = conn.fileSize();

 

Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: EncodedImage.createEncodedImage = IllegalArgumentException

Excellent!!! working like a charm.... Thanks a lot !!!!

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Contributor
pk25
Posts: 17
Registered: ‎06-07-2013
My Device: Blackberry Curve - 9300
My Carrier: ----

Re: EncodedImage.createEncodedImage = IllegalArgumentException

What could be the possible solution for the same problem thst is Illegal Argument Exception when the following is used:

 

Bitmap bmp = Bitmap.createBitmapFromBytes(input, 0, -1,1);
Please use plain text.