【问题标题】:A List of URL to ImageView?ImageView 的 URL 列表?
【发布时间】:2015-11-23 09:48:39
【问题描述】:

我想从一个数组列表中显示图像视图中的图像,该数组包含一个 URL 列表,如

private String imageUrls[] = {"http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                            "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                            "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png"
                            };

当我转到下一张图片时,它会显示OutOfMemoryException

请帮忙

我使用的代码如下

protected Bitmap doInBackground(String... urls) {
              Bitmap bitmap = null;
              try {



          String url = urls[0];

                InputStream in = null;
                try {
                    in = new java.net.URL(url).openStream();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                bitmap = BitmapFactory.decodeStream(in);


              } catch (OutOfMemoryError e) {
                  Log.e("MyApp", e.getMessage());
              }
              return bitmap;  
          }

          protected void onPostExecute(final Bitmap result) {
              handler=new Handler();
              handler.post(new Runnable() { 
                    @Override 
                    public void run() { 

                         bmImage.setImageBitmap(result);

                    } 
                }); 

          }

【问题讨论】:

  • 图片太大了
  • 在 manifest 中使用 largeHeap,但是看看你的图片尺寸...
  • 是的,我需要显示图像。有没有办法处理这个异常?请帮助
  • 看看毕加索图书馆square.github.io/picasso除非真的有必要,否则不要尝试自己下载/缓存/显示图像。

标签: android


【解决方案1】:

在 android 开发者网站上有一个关于这个主题的专门页面: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

为避免 java.lang.OutOfMemory 异常,请检查 解码之前的位图,除非您绝对信任源 为您提供大小可预测的图像数据,适合您 在可用内存范围内。

【讨论】:

    【解决方案2】:

    Android 中的位图可能是此类错误的雷区。通过使用结构良好且经过良好测试的库为您执行此任务,您将节省大量时间。

    Picasso for example,非常好。

    在你的情况下:

    imageUrls[] = {"http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                                "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png",
                                "http://theopentutorials.com/totwp331/wp-content/uploads/totlogo.png"
                                };
    
    Picasso.with(context).load(imageUrls[index]).into(bmImage);
    

    【讨论】:

    • 它的工作,但只显示第一张图像,其他两个没有显示
    • 图片都是一样的。您可能没有注意到其中的区别。
    • 不,先生,我尝试了不同的 url。imageview 显示为白屏
    【解决方案3】:

    我有类似的用例,我必须从外部 URL 下载一堆图片并在单个活动中显示它们。正如@Aashvi 的 cmets 链接所解释的那样,图像占用大约 4*imageheight*imageWidth 字节。所以我开始加载它的缩略图。 (最大高度 300 并保持纵横比)。 我从我的项目中粘贴代码,其中 asynctask 获取图像并制作更小的位图并设置 imageview 和使用的引用。

       protected Bitmap doInBackground(String... urls) {
    
         Bitmap mPic = null;
        try {
    
            mPic = CommonUtils.getThumbnailFromUrl(urldisplay);
        } catch (OutOfMemoryError e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
    
    }
    
    protected void onPostExecute(Bitmap result) {
            if (result != null) {
                bmImage.setImageBitmap(result);
            }
     }
    

    这里是getThumbNailFromUrl的实现

    public static Bitmap getThumbnailFromUrl(String url) {
        Bitmap bitmap = null;
        InputStream input;
        try {
            input = new java.net.URL(url).openStream();
    
            if (input != null) {
                BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
                onlyBoundsOptions.inJustDecodeBounds = true;
                onlyBoundsOptions.inDither = true;//optional
                onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
                BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    
                input.close();
                if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
                    return null;
    
                int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
    
                double ratio = (originalSize > 300) ? (originalSize / 300) : 1.0;
    
                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
                bitmapOptions.inDither = true;//optional
                bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;//optional
                input = new java.net.URL(url).openStream();
                bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
                assert input != null;
                input.close();
            }
    
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    
    }
    

    此代码取自以下 stackoverflow 答案。 How to get Bitmap from an Uri?

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多