2013-05-05 117 views
1

我的服務器上有一張圖片。該圖像的URL是這樣的: http://www.mydomain.com/123456uploaded_image.jpg使用URL設置ImageView

我想將此圖像設置爲我的ImageView。這裏是我試過的代碼:

try{ 
      String url1 = myeventimagearray[position]; 
      URL ulrn = new URL(url1); 
      HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
      InputStream is = (InputStream) con.getInputStream(); 
      Bitmap bmp = BitmapFactory.decodeStream(is); 
      if (null != bmp) 
       iveventimg.setImageBitmap(bmp); 
      else 
       Log.i("Image","Not set"); 

     } catch(Exception e) { 
      e.printStackTrace(); 
     } 

當我嘗試這一點,我的ImageView是空的,也就是說,它不設置圖像視圖和我得到了我的logcat這個系統錯誤:

java.lang.ClassCastException:libcore.net.http.FixedLengthInputStream不能轉換到com.example.eventnotifier.Base64 $ InputStream的

Base46.java是我從網上得知編碼找到的文件和解碼並來自Base64符號。

任何想法,爲什麼我得到這個System.error?

謝謝

回答

1

使用URlImageViewHelper,很會照顧加載URL到ImageView的。

Refer this

很會照顧緩存,加載在後臺等本身。

+0

非常感謝!它的工作:) – Nemin 2013-05-05 21:29:12

+0

乾杯..快樂編碼:) – 2013-05-05 21:42:49

0

你很可能得到一個例外,因爲你正在嘗試做在主線程網絡IO。考慮使用加載器或AsyncTask加載圖像。

檢查你的日誌,我打賭你正在自動生成的catch塊中打印堆棧跟蹤。

new Thread(new Runnable() { 
    public void run() { 
    final Bitmap b = bitmap = BitmapFactory.decodeStream((InputStream)new URL(myeventimagearray[position]).getContent()); 
    iveventimg.post(new Runnable() { 
     public void run() { 
     iveventimg.setImageBitmap(b); 
     } 
    }); 
    } 
}).start(); 
+0

我編輯過的文章給出了關於錯誤的詳細解釋。它是否仍然在主線程上做網絡IO? – Nemin 2013-05-05 21:15:54

+0

您得到的例外是由於其他原因。 – 2013-05-05 21:25:37