2015-10-06 53 views
0

我需要從服務器獲取圖像並將其加載到圖像按鈕。當應用程序片段啓動時,動態加載的圖像超過50張。什麼是最好的方式來做到這一點。目前我使用下面的代碼,當我運行它的應用程序被卡住了,我也跟着Android Loading Image from URL (HTTP)教程,但它不顯示圖像時,有多個圖像。如何從圖像加載圖像到ImageButton

這是代碼我目前使用的時候,但申請凍結

private void setListParentItemInfo(View convertView,final IPTVChannel iptvChannel){ 
ImageButton logoBtn = ((ImageButton) convertView.findViewById(R.id.channelLogo)); 

     String image_url="http://Channel.com.au/ChannelLogos/"+Channel.getLogoName(); 
    logoBtn.setImageBitmap(downloadBitmap(image_url)); 
} 


private Bitmap downloadBitmap(String url) { 
    // initilize the default HTTP client object 
    final DefaultHttpClient client = new DefaultHttpClient(); 

    //forming a HttoGet request 
    final HttpGet getRequest = new HttpGet(url); 
    try { 

     HttpResponse response = client.execute(getRequest); 

     //check 200 OK for success 
     final int statusCode = response.getStatusLine().getStatusCode(); 

     if (statusCode != HttpStatus.SC_OK) { 
      Log.w("ImageDownloader", "Error " + statusCode + 
        " while retrieving bitmap from " + url); 
      return null; 

     } 

     final HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream inputStream = null; 
      try { 
       // getting contents from the stream 
       inputStream = entity.getContent(); 

       // decoding stream data back into image Bitmap that android understands 
       final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 

       return bitmap; 
      } finally { 
       if (inputStream != null) { 
        inputStream.close(); 
       } 
       entity.consumeContent(); 
      } 
     } 
    } catch (Exception e) { 
     // You Could provide a more explicit error message for IOException 
     getRequest.abort(); 
     Log.e("ImageDownloader", "Something went wrong while" + 
       " retrieving bitmap from " + url + e.toString()); 
    } 

    return null; 
} 

我怎樣才能解決這個或其他任何理由這樣做呢?

+1

我建議使用[畢加索庫(http://square.github.io/picasso/) –

+0

本教程將幫助你實現畢加索圖書館[使用Android圖像庫畢加索](https://androidcodegeeks.com/working-with-android-image-library-picasso/) –

回答

2

使用Picasso Library

private void setListParentItemInfo(View convertView,final IPTVChannel iptvChannel){ 
ImageButton logoBtn = ((ImageButton) convertView.findViewById(R.id.channelLogo)); 

String image_url="http://Channel.com.au/ChannelLogos/"+Channel.getLogoName(); 
Picasso.with(context).load(image_url).placeholder(yourDrawableImage).into(logoBtn); 
} 
+0

你知道如何添加加載圖像到這個,因爲這個任務3s或4s加載圖像? –

+0

檢查我的更新回答.. –

+0

這個圖書館比Glide和其他人好... –

2

表現不錯,它的凍結,因爲它是在同一個線程的UI(主線程)運行。總是使用不同的線程(例如,AsyncTask)從網絡加載東西,更新的API級別不應該允許你這樣做。 但在這種情況下,你肯定應該和Picasso庫一起去。

0

使用類似facebook的壁畫等圖書館。 http://frescolib.org/

使用此,你不需要從外部下載圖像。 只需設置圖像傳遞uri。它會自動下載並將其設置爲視圖。

2

如果你使用小圖像,那麼你可以通過使用像下面這樣的異步任務來加載圖像。如果您有大量的圖像,那麼你需要按照http://developer.android.com/intl/es/training/displaying-bitmaps/load-bitmap.html

class ImageLoader extends AsyncTask<String, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(String... params) { 
     return getBitmapFromURL(params[0]); 
    } 

    @Override 
    protected void onPostExecute(Bitmap result) { 
     super.onPostExecute(result); 
     imageButton.setImageBitmap(result); 
    } 

} 

public Bitmap getBitmapFromURL(String imageURL) { 
    try { 
     URL url = new URL(imageURL); 
     HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     // Log exception 
     return null; 
    } 
} 
相關問題