Search code examples
blackberryblackberry-simulatorblackberry-eclipse-plugin

Illegal Argument Exception when trying to convert byte to Bitmap in blackberry


Here is my code where i am getting profile image bytes from twitter api,

new Thread() {

    public void run() {
        byte dbata[] = getBitmap(profle_pic_str);
        if (dbata != null) {
            EncodedImage bitmap_img = EncodedImage.createEncodedImage(dbata, 0, -1);
            Bitmap image =bitmap_img.getBitmap();
            final Bitmap profle_pic_bmp = image;
            final Bitmap scld_bmp = new Bitmap(90, 100);
            Application.getApplication().invokeLater(new Runnable() {
                public void run() {
                    if (profle_pic_bmp != null) {
                        profle_pic_bmp.scaleInto(scld_bmp, Bitmap.FILTER_LANCZOS);
                        phot_profle.setBitmap(scld_bmp);
                    } else {
                        Dialog.alert("null");
                    }
                }
            });
        // } else {
            // Dialog.alert("bytes are null");
        }
    }
}.start(); 

Here i have method getBitmap(profle_pic_str); which returning bytes array of image,

public byte[] getBitmap(String url) {
    Bitmap bitmap = null;
    String strg = HttpConnector.getConnectionString();
    byte b[] = null;
    try {
        b = getXML(url + strg);
    } catch (IOException ie) {
        ie.printStackTrace();
    }
    return b;
} 

the url which i used is this one

http://api.twitter.com/1/users/profile_image?screen_name=screen_nameof_user&size=bigger

public byte[] getXML(String url) throws IOException {
    ContentConnection connection = 
        (ContentConnection) javax.microedition.io.Connector.open(url);
    java.io.DataInputStream iStrm = connection.openDataInputStream();
    java.io.ByteArrayOutputStream bStrm = null;
    byte xmlData[] = null;
    try {
        // ContentConnection includes a length method
        int length = (int) connection.getLength();
        if (length != -1) {
            xmlData = new byte[length];
            // Read the png into an array
            // iStrm.read(imageData);
            iStrm.readFully(xmlData);
        } else // Length not available...
        {
            bStrm = new java.io.ByteArrayOutputStream();
            int ch;
            while ((ch = iStrm.read()) != -1) bStrm.write(ch);
            xmlData = bStrm.toByteArray();
            bStrm.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Clean up
        if (iStrm != null) iStrm.close();
        if (connection != null) connection.close();
        if (bStrm != null) bStrm.close();
    }
    return xmlData;
} 

When i am trying to convert byte array to EncodedImage

EncodedImage  bitmap_img = EncodedImage.createEncodedImage(dbata, 0, -1);

in this line of code i am getting illegal argument exception.

same code is working for Facebook profile image. I dont know why this code giving error when i am doing for twitter. Please help me friends.


Solution

  • try this -

    EncodedImage _encoded_img = EncodedImage.createEncodedImage(dbata, 0, dbata.length);
    

    On your code,

    EncodedImage bitmap_img = EncodedImage.createEncodedImage(dbata, 0,-1);
    

    -1 is the length of the array. Its not static. Change -1 to dbata.length.