2013-11-01 119 views
1

您好我看過類似的問題,但我無法解決問題。它不工作。 我想從服務器下載一些圖像,並將它們放在android應用程序中的ImageViews中。Android - 從服務器下載圖像並加載到ImageView中

public class MyAsyncTask extends AsyncTask { 
    protected String doInBackground(String... params) { 
    ........... 
    URL urlImagine = new URL("http://s.ytimg.com/yts/img/favicon-vfldLzJxy.ico"); 
    //Version 1: 
    URLConnection conn = urlImagine.openConnection(); 
    inputStreamImagine = (InputStream)conn.getContent(); 
    .......... 
    //version 2: 
    inputStreamImagine = urlImagine.openStream(); 
    bufImagine = new BufferedInputStream(inputStreamImagine); 
    ......... 
    } 
} 
    //Some other class: 
    .............. 
    //http://stackoverflow.com/questions/15569953/download-image-from-image-url-to-image-view 
    //version 1: 
    //Download Image From Image URL to image view 
    if (inputStreamImagine!=null){ 
     Bitmap bitmap = BitmapFactory.decodeStream(inputStreamImagine);  
     imageView.setImageBitmap(bitmap); 
    } 
    //version 2: 
    if (bufImagine!=null){ 
     // Convert the BufferedInputStream to a Bitmap 
     Bitmap bMap = BitmapFactory.decodeStream(bufImagine); 
     Drawable drawable = new BitmapDrawable(bMap); 
     imageView.setBackgroundDrawable(drawable); 
     //iv.setImageDrawable(drawable);  
    } 

我試過使用這兩個示例,但ImageView不加載任何圖像。 你能幫我嗎?

+0

試試這個例子:http://androiddevelopmentworld.blogspot.in/2013/04/how-to-display- image-from-url-in-android.html –

+0

@Prince。謝謝。這個例子很清楚,我正在追蹤它,但我不明白爲什麼它不適合我。 – Alex

回答

1

您應該使用延遲加載concept.You應該查看下面的鏈接:

Lazy load of images in ListView

+0

我也在使用ListView,所以我認爲你已經指出了我的確切問題。這個鏈接非常有趣,可能會讓我找到解決問題的辦法。 – Alex

+0

所以PLZ標記爲接受:)。 –

+0

是的,當然,我打算這麼做,只是我還在努力。謝謝Himanshu。你幫了我很多。問題是他們沒有在ListView中加載。 – Alex

相關問題