4

I need to convert large view (width and height as 1920x840) to Bitmap. I am getting java.lang.OutOfMemoryError exception on low end devices whose memory size less. I tried this code.

        setDrawingCacheEnabled(true);
        buildDrawingCache();
        Bitmap b = getDrawingCache();
        if (b == null) {
            int width = getLayoutParams().width;
            int height = getLayoutParams().height;
            //here i am getting out of memory exception
            b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
            Canvas c = new Canvas(b);
            draw(c);
        }

        destroyDrawingCache();
        setDrawingCacheEnabled(false); 
        return b;
Naresh Palle
  • 339
  • 3
  • 8
  • Possible duplicate https://stackoverflow.com/questions/7200535/how-to-convert-views-to-bitmaps – duggu Feb 23 '18 at 11:33
  • @duggu, it is not – Vladyslav Matviienko Feb 23 '18 at 12:09
  • thats why im not close this question @VladyslavMatviienko, so i wrote possible duplicate – duggu Feb 23 '18 at 12:10
  • Form [this](https://stackoverflow.com/q/30961921/717341) older question (which might equate to today's "low-end" devices) it seems that the `buildDrawingCache()` is the problem. – Lukas Knuth Feb 23 '18 at 12:47
  • We can scale the bitmap if we create it from resource, file or byte array. but here i need to draw whole view into the bitmap. so bitmap size should be same as a view size. Here two culprits are: getDrawingCache() returns null for big sizes and Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); throws out of memory exception. Is there any other way to create bitmap from view? – Naresh Palle Feb 23 '18 at 13:33

0 Answers0