On some devices, it is not possible to get Bitmap from View via getDrawingCache() . Not at all. On 4.4.4 SGSIII - everything is normal, and on 4.1.2 the GT-S7582 - fails.

Rather, the function itself is executed, but when I try to find out the height or width of the resulting Bitmap , I swear at java.lang.NullPointerException .

ps.Sam View - more than the screen size of the device (in both cases).

 RelativeLayout rl_timer_to_send = (RelativeLayout) c.getWindow().findViewById(R.id.rl_timer_to_send); rl_timer_to_send.setVisibility(View.VISIBLE); rl_timer_to_send.setDrawingCacheEnabled(true); Bitmap layer_timer = rl_timer_to_send.getDrawingCache(); float resizeTimerFactor = 851.0f / layer_timer.getWidth(); // java.lang.NullPointerException 

1 answer 1

This code should work:

 RelativeLayout rl_timer_to_send = (RelativeLayout) c.getWindow().findViewById(R.id.rl_timer_to_send); rl_timer_to_send.setVisibility(View.VISIBLE); rl_timer_to_send.setDrawingCacheEnabled(true); rl_timer_to_send.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); rl_timer_to_send.layout(0, 0, rl_timer_to_send.getMeasuredWidth(), rl_timer_to_send.getMeasuredHeight()); rl_timer_to_send.buildDrawingCache(true); Bitmap layer_timer = rl_timer_to_send.getDrawingCache(); float resizeTimerFactor = 851.0f / layer_timer.getWidth(); 

The source of the answer is here .

  • I tried this method. Just on older devices does not work ... - Chekist
  • @Chekist, strange .. I did not expect that it would work incorrectly on older devices. - nick