How to avoid null pointer in View.getDrawingCache ()?

My application has this piece of code

mView.setDrawingCacheEnabled(true); mBitmap = Bitmap.createBitmap(mView.getDrawingCache()); 

Where mView is an instance of a custom subclass of FrameLayout. The app has been working well for 6 months with thousands of users. Recently, I got several reports (possibly from the same user, but I can’t confirm) about the empty pointer passed to createBitmap (). I do not know which hardware or version of Android you are using.

What can cause getDrawingCache () to return null even if I include the cache directly in front of it? Is this related to hardware acceleration in new phones? Is it due to insufficient ram for the bitmap? Anything else? Can this be prevented?

Edit: I found this solution here http://tinyurl.com/7yranvj . Is this a smart decision?

 mBitmap = Bitmap.createBitmap(mView.getWidth(), mView.getHeight(), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(mBitMap); mView.draw(canvas); 
+6
source share
1 answer

in fact, your hunch was correct. If you use a hardware accelerated layer, you cannot use the drawing cache. By default, new devices have hardware acceleration, so this may be your problem. You can disable hardware acceleration for one view.

 if(android.os.Build.VERSION.SDK_INT >= 11) frame.setLayerType(LAYER_TYPE_SOFTWARE, null); frame.setDrawingCacheEnabled(true); 
0
source

Source: https://habr.com/ru/post/916523/


All Articles