2015-10-06 31 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; 
    } 
} 
相关问题