-1

I am displaying Images on RecyclerView in ImageView with dimensions as 50*50. I have used the guide Guide given by google to display Bitmaps using Scaling etc.Along, with guide I have also use Glide library to set Image.The code to Sample Image is

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

This is a code to 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);
    }  

This is code to set Image to ImageView using Glide

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

In-spite of doing all this calculations I am always getting OutOfMemory error and app get crashed when more Images are load. How to resolve this ?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Satyam Gondhale
  • 1,415
  • 1
  • 16
  • 43

1 Answers1

0

For your base64String to Imageview use it like this

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

I Hope it will works fine

Nikunj Paradva
  • 15,332
  • 5
  • 54
  • 65