0

I have a method to resize bitmap decoded from a file path or create s thumbnail by using ThumbnailUtils. then to set the bitmap as a background for a custom view or draw on canvas.

in few cases it cause OutOfMemoryError

java.lang.OutOfMemoryError: 
  at dalvik.system.VMRuntime.newNonMovableArray (VMRuntime.java)
  at android.graphics.Bitmap.nativeCreate (Bitmap.java)
  at android.graphics.Bitmap.createBitmap (Bitmap.java:942)
  at android.graphics.Bitmap.createBitmap (Bitmap.java:913)
  at android.graphics.Bitmap.createBitmap (Bitmap.java:844)
  at android.media.ThumbnailUtils.transform (ThumbnailUtils.java:425)
  at android.media.ThumbnailUtils.extractThumbnail (ThumbnailUtils.java:228)
  at android.media.ThumbnailUtils.extractThumbnail (ThumbnailUtils.java:203)

mostly caused by ThumbnailUtils maybe because I'm not scaling down the bitmap before extracting thumbnail?

how to do this without loading more on memory and in general how this methods can be more optimized

// w = view Width, h = view Height
public BitmapDrawable decodeBitmap(int w, int h) {
    Bitmap bitmap = null;
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = false;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        options.inDither = true;
        bitmap = BitmapFactory.decodeFile(BackgroundImage,options);
        if(scaleType==0) { //center crop
            bitmap = cropCenter(bitmap,w,h);
        }else if(scaleType==1) { //fitxy
            bitmap = resize(bitmap,w,h);
        }
        return new BitmapDrawable(mContext.getResources(), bitmap);
    } catch (Exception e) {

    }
    return null;
}

private Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
    try{
        if (maxHeight > 0 && maxWidth > 0) {
            int width = image.getWidth();
            int height = image.getHeight();
            float ratioBitmap = (float) width / (float) height;
            float ratioMax = (float) maxWidth / (float) maxHeight;

            int finalWidth = maxWidth;
            int finalHeight = maxHeight;
            if (ratioMax > ratioBitmap) {
                finalWidth = (int) ((float) maxHeight * ratioBitmap);
            } else {
                finalHeight = (int) ((float) maxWidth / ratioBitmap);
            }
            image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, false);
            return image;
        }
    } catch (Exception e) {

    }
    return image;
}
private Bitmap cropCenter(Bitmap bmp, int maxWidth, int maxHeight) {
    try{
        return ThumbnailUtils.extractThumbnail(bmp, maxWidth, maxHeight);
    } catch (Exception e) {

    }
    return bmp;
}

I'm using BitmapDrawable to use setBackground(Drawable);

code ketty
  • 194
  • 2
  • 12

1 Answers1

0

You can use glide for it. It increases performance and reduce lagging while processing

This https://github.com/bumptech/glide is the repository of glide.

You can find example here https://futurestud.io/tutorials/glide-image-resizing-scaling related to resize. Below content is get from before mention site to explain how to resize image

with Glide 4

GlideApp  
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
    .into(imageViewResize);

with Glide 3

Glide  
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .override(600, 200) // resizes the image to these dimensions (in pixel). resize does not respect aspect ratio
    .into(imageViewResize);
PushpikaWan
  • 2,437
  • 3
  • 14
  • 23
  • thank you but I'm looking for a native method to handle a bitmap without using a library and I'm using it as a background for a custom view – code ketty Nov 04 '18 at 10:54
  • @codeketty as far as I know, android deal images with different resolution poorly. That's why this library is introduced. You can use it as background image loader easily. I think you can go through it and serach about android image loading. Then take a decission what u want. Happy coding – PushpikaWan Nov 04 '18 at 10:56
  • I want it as a bitmap – code ketty Nov 04 '18 at 11:02
  • @codeketty nevigate to answer of this https://stackoverflow.com/questions/42278134/is-there-a-way-to-load-image-as-bitmap-to-glide – PushpikaWan Nov 04 '18 at 11:04
  • thank you but I'm looking for a native method to handle a bitmap without using a library and I'm using it as a background for a custom view – code ketty Nov 04 '18 at 11:08