0

这是我从 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(profile_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 做这个代码时会出错。请朋友们帮帮我。

4

1 回答 1

0

试试这个 -

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

在你的代码上,

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

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

于 2012-11-08T09:35:49.287 回答