尝试在黑莓中将字节转换为位图时出现非法参数异常

时间:2012-11-08 09:23:55

标签: blackberry blackberry-simulator blackberry-eclipse-plugin

这是我的代码,我从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(); 

这里有方法getBitmap(profle_pic_str);返回图像的字节数组,

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

我使用的网址是这个

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

当我尝试将字节数组转换为EncodedImage

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

在这行代码中我得到了非法的参数异常。

相同的代码适用于Facebook个人资料图片。我不知道为什么这个代码在我为twitter做的时会出错。请帮帮我朋友。

1 个答案:

答案 0 :(得分:0)

试试这个 -

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

在你的代码上,

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

-1是数组的长度。它不是静止的。将-1更改为dbata.length。