【问题标题】:How to load images to ImageButton from url如何从 url 将图像加载到 ImageButton
【发布时间】:2015-10-06 06:45:18
【问题描述】:

我需要从服务器获取图像并将其加载到图像按钮。应用程序片段启动时动态加载的图像超过 50 个。最好的方法是什么。目前我正在使用以下代码,当我运行它时,应用程序卡住了,我也跟着Android Loading Image from URL (HTTP)教程,但是当有多个图像时它不显示图像。

这是我当前正在使用的代码,但应用程序正在冻结

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;
}

我该如何解决这个问题或任何其他为什么要这样做?

【问题讨论】:

标签: android image


【解决方案1】:

使用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);
}

【讨论】:

  • 你知道如何添加加载图像,因为这个任务 3s 或 4s 加载图像?
【解决方案2】:

使用 picaso 库,它易于使用,速度快,节省现金以备将来使用。

http://square.github.io/picasso/

【讨论】:

    【解决方案3】:

    是的,它冻结了,因为它与 UI(主线程)在同一个线程中运行。始终使用不同的线程(例如AsyncTask)从网络加载某些内容,较新的 API 级别根本不允许您这样做。 但在这种情况下,您绝对应该使用 Picasso 库。

    【讨论】:

      【解决方案4】:

      如果您使用小图像,则可以使用如下异步任务加载图像。如果你有大图,那么你需要关注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;
          }
      }
      

      【讨论】:

        【解决方案5】:

        我想你应该像 How to use separate thread to perform http requests 中显示的那样为每个图像使用多个线程

        【讨论】:

          【解决方案6】:

          使用 facebook 的 fresco 等库。 http://frescolib.org/

          使用这个你不需要从外部下载图像。 只需设置图像传递 uri。它会自动下载并设置到视图中。

          【讨论】:

            猜你喜欢
            • 2017-07-13
            • 2011-05-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-03-21
            • 1970-01-01
            相关资源
            最近更新 更多