1
I have a function that takes a view and creates a bitmap. follow the code:
public static Bitmap getBitmapFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
The problem is that "buildDrawingCache" apparently does not work on API 28 (Android 9).
Is there any way to make the view bitmap without using "buildDrawingCache"?
I didn’t even know about
DrawingCache
, so much that I avoided giving an answer to the question. I just read in documentation that this cache is obsolete since API 11 and its use may compromise application performance.– Augusto Vasques
Everything worked fine, I tested on Apis 26,27,28 and all the code worked. Just to cool what the ramaral had already said, do not try to use this code block inside the onCreate because it will not work.
– Matheus Suffi