这是我的代码,在其中我从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(prifle_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;
}
我使用的是这个URL
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 bitmap_img = EncodedImage.createEncodedImage(dbata, 0, -1);
在这条代码中,我得到了非法参数例外。
相同的代码正在用于Facebook个人资料图像。我不知道为什么当我为Twitter做错误时,为什么会出现错误。请帮助我的朋友。
尝试这个 -
EncodedImage _encoded_img = EncodedImage.createEncodedImage(dbata, 0, dbata.length);
在您的代码上,
EncodedImage bitmap_img = EncodedImage.createEncodedImage(dbata, 0,-1);
-1是阵列的长度。它不是静态的。更改-1为dbata.length。