Call to `getDrawingCache` returns null when scroll is enabled
Asked Answered
T

1

6

What is missing in this code? This same code works on ICS. On API 8 the scroll appears and some content goes out of screen. How to get the drawing cache in this case?

Code:

TableLayout page = (TableLayout) findViewById(R.id.page);
page.setDrawingCacheEnabled(true);
page.buildDrawingCache();

// getDrawingCache returns null...
Bitmap pageBmp = Bitmap.createBitmap(page.getDrawingCache(true));
page.destroyDrawingCache();
page.setDrawingCacheEnabled(false);
Tighten answered 19/7, 2012 at 12:26 Comment(2)
Possible duplicate of Android View.getDrawingCache returns null, only nullMotivation
similar #2339929Motivation
T
18

I solved it. Created a bitmap of view size and drew the view into it.

TableLayout page = (TableLayout) findViewById(R.id.page);
Bitmap pageBmp = Bitmap.createBitmap(page.getWidth(), page.getHeight(), 
                                       Config.ARGB_8888);
Canvas canvas = new Canvas(pageBmp);
page.draw(canvas);

Used the pageBmp bitmap..

Tighten answered 19/7, 2012 at 13:5 Comment(2)
10x a lot man, i had some problems with a bitmap, it was always null, with something similar as in how you tried the first time, but this code just saved my project :D It works flawlessAfghani
Awesome hack dude.... Solved my problem. This would really help those who are using scrollview as a parent because getDrawingCache() doesn't work if scrollView is present.Psychopath

© 2022 - 2024 — McMap. All rights reserved.