Java:从UTF8字符串创建Drawable

时间:2011-08-12 10:35:12

标签: java android image drawable

我有以下代码:

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

这是我得到的响应字符串(来自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}Ñ?ª¯:}¡+ +μ3ötÇàÕ    AI#$a7ÙÝd¾H'
  £²$8ÝÏ«äÙßîóï{|,²Ê2)英哩,²Ê2)Ë��¦,² E2)英哩,²@¬dg¡ä< d°BsìJvVHÖpüõÐí°I·XH÷1×ÁJkÅâbQ §3PÅKãôÊp)¬¬{AO°¡0ªÄR¬>MÎý0uýËâ/ |ÖÊÌÊöðèCUWl»CEAC〜¸ÈIVRKþ>wKÑgX]góY¼¢
  12³²Å{ZB型\ o«&安培;0吨]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¯ÅÝÀÔ¤TYuÖ*,H_EÕ3μÝ|ïÉkM#Q}Â_¤Ê©%|¤·E,AA&GT;Á4yμÁ^} 30×PU /©RLwDáov] \IaÔIϧÐy|7ÜÖÅ÷4q®#ISD&LT;〜+吨;óF¡Ô%|¤ÄtÐiÆ&gt;一种$ 9;nÏq|øùóçs}Âç¡ä,GtFGènWj½rºlXôóªð½&安培;3qofê   08-12 11:33:52.223:INFO / EOH(10244):   IÎY\¤s[6¯klÕÖrqºÊê{*ôÀ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Ö{ÁeSLY0eÀeSLY0eÀeSLY0eÀe§〜) L1 +IEND®B`

正如您所看到的,我正在尝试将名为“response”的String转换为Drawable对象。麻烦的是,图像始终是空白的!

有谁知道我可以解决这个问题?

非常感谢,


编辑:

这也不起作用;(

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

2 个答案:

答案 0 :(得分:2)

试试这个:

BitmapDrawable drawable = BitmapFactory.decodeFile(response);

如果您不需要它,可以执行以下操作。

Bitmap bm = BitmapFactory.decodeFile(response);
ImageView captcha = (ImageView)findViewById(R.id.imageView2);
captcha.setImageBitmap(bm);

<强> EDITED

对于在线文件,您可以使用:

BitmapFactory.decodeStream(is)

答案 1 :(得分:0)

这是我正在使用的完整功能(不起作用......):

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) {

            }
        });
     }