Nullpointerexception In Createbitmap() From View
I am using the below code for getting a Bitmap from a View : private static Bitmap loadBitmapFromView(View yourView) { Bitmap snapshot = null; Drawable drawable = n
Solution 1:
Try this way
privatestatic Bitmap loadBitmapFromView(View yourView) {
Bitmapsnapshot=null;
Drawabledrawable=null;
yourView.setDrawingCacheEnabled(true);
yourView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH); //Quality of the snpashottry {
yourView.buildDrawingCache(); // UPDATE HERE
snapshot = Bitmap.createBitmap(yourView.getDrawingCache());
drawable = newBitmapDrawable(snapshot);
} finally {
yourView.setDrawingCacheEnabled(false);
}
return snapshot;
}
Explanation :
For the knowledge Bitmap is final class. So it's priority for garbage collector(GC) is very low and it is very memory expensive.By default view does not creates the bitmap from the canvas because if it do so one bitmap will be generate in memory for each view inflated and it may cause buildDrawingCache. So buildDrawingCache()
method is given that you can generate the bitmap when it is required.
Solution 2:
Should the code be
snapshot = snapshot.createBitmap(yourView.getDrawingCache());
as youve already cast snapshot as Bitmap there
Bitmapsnapshot=null;
?
Post a Comment for "Nullpointerexception In Createbitmap() From View"