I have the following code:
public void onSuccess(String response) {
Log.i("EOH",response);
Drawable d = null;
Object content=response.getBytes();
InputStream is = (InputStream)content;
d = Drawable.createFromStream(is, "src");
ImageView captcha = (ImageView)findViewById(R.id.imageView2);
captcha.setImageDrawable(d);
}
Here is the response string I'm getting (from LogCat):
08-12 11:33:52.223: INFO/EOH(10244): PNG
08-12 11:33:52.223: INFO/EOH(10244): 08-12 11:33:52.223: INFO/EOH(10244): ������ IHDR������d������2������%Wéé����IDAThíéSgÇÏÙM²\R 08-12 11:33:52.223: INFO/EOH(10244): *h=¨xß-¥:Ni}Ñ?ª¯:}¡S+µ3ötÇàÕ Äî#$a7ÙÝd¾H' £²$8ÝÏ«äÙßîóï{��¦,²Ê2)Ë��¦,²
Ê2)Ë��¦,²
Ê2)Ë��¦,²@¬dg¡ä<¯ð°BsìJvVHÖpüõÐí°I·xH÷1×ÁJkÅâbQ§3PÅKãôÊp)¬¬{áö°¡0ªÄR¬>MÎý0uýËâ/|ÖÊÌÊöðèCUW��l»cëaç~¸ÈIVRKþ>wKÑgX]góY¼¢ 12³²Å{zB\o«&0��t]oìŹ¢³ß}§êªYXiêwq;�� ä 08-12 11:33:52.223: INFO/EOH(10244): I.}#Bh+»¹=ÒNF3Ø}��-ÞSíëAá¯N]*ô5q·¿^¾0þÅÝW.sÌ$§åÍJ.¦^ïtl;P¸@¸tãOèPr~D 08-12 11:33:52.223: INFO/EOH(10244): fÝÆFÄ ?¸1û��¬¡KÓdMÔÈ)��pìaç~��_Ø@lðU|¸ïkâ²g·\³ .©¤+�� .[kóÙqû¥Ò#Îç¼g2G¥ñË?^j½:}=(cÎCé«d!ÔóÉpºqLPrÞШÎxiÔHòE¾© H#}ÂÀ{ ��tDHýQcÁö¬J¯ÅÝÀÔ¤
TY��uÖ*,H_EÕ3µÝ|ïÉkM#Q}Â_��¤Ê©%¦¤·Ê��,Áä>Á4yµÁ^}3ô×PÜ/©RLwDáov]\IaÔIϧ��Ðy|7ÜÖÅ÷4q®#ÎsШ<~+t;óF¡Ô%¦¤ ÄtÐiÆ>ä$9;nÏq¦øùóçs}Âç¡ä,GtFGènWj½rºlXôóªð½&3qofê 08-12 11:33:52.223: INFO/EOH(10244): IÎY\¤s[6¯klÕÖr�� qºÊê{*ôÀNǶlâ¨I®!¬© %ÃÝ|/ðþÙýèáÙÃèßq¤*brPýK=S��wÂmAiÔE:wséKϧXÖ-"����GxV;B¨ÙtÚÓÜâ=UJ¤¢½*c{±ä��dM+KQ9UFcô¨<~yòZ*G~6¥UÖJ;n©q��øÌu4SA7ßFRp£½ÆMºl¸5©+CqP£0ª.{Ó3_ÅýSÝgÈüÒ%¤äÊ@%]qÖÛúôiºöÓô!Ñß'ìá>YÞ4ó#!t°pßËøt¦>/i»ý6{s*1ÓÍ÷f¶üÜs¨tËh×dbÅ ´ 46!OtõÈêî))(��ÓeéÂVJ~EK.{y;î4°u lÝâv¯ÅóMÙ×Sǫ̀4Q¢¢&p®M̬Ä_gî§DlwlÙÇíÎ:��³¬·U÷ÇÄ^ôÇ5Ðö49ÉÂtAjeeÕcj��¾WèlXLy)ïÛk.µøÅÀlrNÓu'Éù¬ë.z¦¶?öüN¸MÖä¬EE,^��ÅW½¬¥ZkóßRßwçÛÆå ��J£U¶ÊT£®ëÊ��°9ü4´*NóË 5¶uM;³Ròe|XÑ��è>Õÿ;ßO\JÌ��@[¸CÓµåuºVQÞ_\IÌòª
ŬVÜj¤qÒA'Ý#úÿüóO×uAO4ʼ²Êñ¨ø¡fÖ{Áe��SLY0eÀe��SLY0eÀe��SLY0eÀe§~)l1+��������IEND®B`
As you can see, I'm trying to convert a String called 'response' to a Drawable object. Trouble is, the image is always blank!
Does anyone 开发者_如何转开发know of any way I might be able to fix this?
Many thanks in advance,
Edit:
This didn't work either ;(
@Override
public void onSuccess(String response) {
Log.i("EOH",response);
ByteArrayInputStream bis = new ByteArrayInputStream(response.getBytes());
Bitmap bm=BitmapFactory.decodeStream(bis);
ImageView imgView = (ImageView)findViewById(R.id.imageView2);
imgView.setImageBitmap(bm);
}
Try this:
BitmapDrawable drawable = BitmapFactory.decodeFile(response);
Or you could do the following, if you don't need it to be Drawable.
Bitmap bm = BitmapFactory.decodeFile(response);
ImageView captcha = (ImageView)findViewById(R.id.imageView2);
captcha.setImageBitmap(bm);
EDITED
For online files, you can use this:
BitmapFactory.decodeStream(is)
Here's the full function I'm using (doesn't work...):
private void captchaLoad() {
AsyncHttpClient myClient = new AsyncHttpClient();
PersistentCookieStore myCookieStore = new PersistentCookieStore(context);
myClient.setCookieStore(myCookieStore);
RequestParams params = new RequestParams();
myClient.post("http://www.extremetech.com/wp-content/uploads/2011/08/amd-logo-106x59.jpg", params, new AsyncHttpResponseHandler() {
@Override
public void onStart() {
}
@Override
public void onSuccess(String response) {
Log.i("EOH",response);
ByteArrayInputStream bis = null;
try {
bis = new ByteArrayInputStream(response.getBytes("UTF8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap bm=BitmapFactory.decodeStream(bis);
ImageView imgView = (ImageView)findViewById(R.id.imageView2);
imgView.setImageBitmap(bm);
}
@Override
public void onFailure(Throwable e) {
}
});
}
精彩评论