【问题标题】:OutOfMemory error while loading Images using Sampling使用采样加载图像时出现 OutOfMemory 错误
【发布时间】:2019-05-17 11:21:42
【问题描述】:

我在ImageView 中的RecyclerView 上显示Images,尺寸为50*50。我已经使用谷歌给出的指南Guide 来显示位图使用缩放等。随着指南,我还使用Glide 库来设置图像。示例图像的代码是

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 2;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) >= reqHeight
                    && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }  

这是 DecodeSampleBitmap 的代码

public static Bitmap decodeSampledBitmapFromResource(int reqWidth, int reqHeight,byte[] bytes) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inInputShareable=true;
        options.inPurgeable=true;
       // BitmapFactory.decodeResource(res, resId, options);
        BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
    }  

这是使用 Glide 将 Image 设置为 ImageView 的代码

BitmapFactory.Options options = new BitmapFactory.Options();
                BitmapFactory.decodeByteArray(imageBytes,0,imageBytes.length,options);
                options.inJustDecodeBounds = true;
                options.inInputShareable=true;
                options.inPurgeable=true;
                int imageHeight = options.outHeight;
                int imageWidth = options.outWidth;
                String imageType = options.outMimeType;
                Drawable d=new BitmapDrawable(getResources(),decodeSampledBitmap(50,50));
                Glide.with(getActivity())
                        .load(d).into(offerBinding.restImg);  

尽管进行了所有这些计算,但我总是收到OutOfMemory error,并且当加载更多图像时应用程序会崩溃。如何解决?

【问题讨论】:

  • 您是否在manifest 的应用程序标签中添加了android:largeHeap="true"
  • 您从哪里获取图像?在线还是从内部存储?
  • 是的。我在 Manifest 中为 true 添加了大 heah
  • 我正在使用 Api 调用从服务器获取图像

标签: android android-glide android-bitmap bitmapfactory


【解决方案1】:

对于您的 base64StringImageview,请像这样使用它

Glide.with(context)
.load(Base64.decode(base64String, Base64.DEFAULT))
.placeholder(R.drawable.placeholder) // load at start
.error(R.drawable.imagenotfound) // in case of error
.override(50,50)  
.skipMemoryCache(true)
.into(rowImageView);

希望一切顺利

【讨论】:

  • 图像缩放等呢?
  • 你得到答案了吗?
  • 没有。还是一样的内存问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 2016-08-28
  • 2011-10-21
  • 2012-10-23
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多