2011-01-08 57 views
0

嘗試從URL顯示JPG時,出現圖像出現在ImageView中的問題。我可以打開URLConnection,在InputStream中拉下圖像,將該圖像解碼爲位圖。我甚至可以獲得位圖高度和寬度的值。我將位圖設置爲ImageView,仍然可以獲得Drawable的高度。但是,該圖像仍然沒有出現在我的應用程序中。任何想法,我可能會失蹤?謝謝你的幫助。ImageView中未出現位圖

try{ 
    URL imgURL = new URL(imgLocation); 
    URLConnection conn = imgURL.openConnection(); 
    conn.connect(); 
    InputStream is = conn.getInputStream(); 

    BufferedInputStream bis = new BufferedInputStream(is, 25); 

    Bitmap bm = BitmapFactory.decodeStream(bis); 

    if(bm != null){ 
     System.err.println("Image Height: " + bm.getHeight()); 
     System.err.println("Image Width: " + bm.getWidth()); 
    } else { 
     System.err.println("bm is null!!!"); 
    } 

    img.setImageBitmap(bm); 
    System.err.println("Drawable Height: " + img.getDrawable().getIntrinsicHeight()); 

    } catch (IOException e) { 
     // Print out the exception that occurred 
     e.printStackTrace(); 
    } 

回答

0

我沒有直接回答您的問題,但我可以爲此需求提供其他解決方案。

使用ImageView會導致很多問題,可用性。 您需要避免屏幕鎖定,並從優化的角度重新下載圖像是不好的。

我建議簡單很多&高效的解決方案;你可以使用Droid-Fu的WebImageView。 它像一個魅力。您只需傳遞圖像的URL,然後讓WebImageView處理下載,顯示進度甚至緩存。

這裏的Droid-Fu的GitHub上playgorund(搜索 '機器人福')

附:我也對這個緩存機制做了一些擴展,已經在我的github操場上發佈(搜索'wareninja')