0

I need to capture a view as image, but the view is not located in my activity's XML file, it's located in layout_report.xml file.

I can capture a view that is located in activity xml with the method I wrote (method signature: public Bitmap saveViewAsImage(View view, float pageWidth, float pageHeight).

I tried to inflate view when the activity is created with below code:

LayoutInflater li = LayoutInflater.from(this);
View layout = li.inflate(R.layout.layout_report, null, false);
saveViewAsImage(viewReport, 2600, 2800);

However, since view height and width was 0, I received an error. I have also tried to use fragment and inflate XML, but since I didn't call fragment beginTransaction, view was null.

How can I prepare a view that has width and height?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • 1
    https://stackoverflow.com/questions/2801116/converting-a-view-to-bitmap-without-displaying-it-in-android – ADM Sep 09 '21 at 04:53
  • thank you @ADM , ` int specWidth = View.MeasureSpec.makeMeasureSpec(0 /* any */, View.MeasureSpec.UNSPECIFIED); view.measure(specWidth, specWidth); int viewWidth = view.getMeasuredWidth(); int viewHeight = view.getMeasuredHeight(); ` height and width is not zere but is not correct too , its width is way more larger than its height – ocr practice Sep 09 '21 at 06:35
  • 1
    hi there @ADM, finally found the solution from your link, this answere [link](https://stackoverflow.com/a/54128490/7190108) solved m thank you for your help and directing me to right direction , since my rate is low i cant up vote you, my apology, have a nice day :) – ocr practice Sep 09 '21 at 12:52
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 14 '21 at 17:09

0 Answers0