Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Thanks to you guys, I've finally found out what was wrong.</p> <p><code>View v = view.getRootView();</code> should not be used because it will call the root view which I do not want. I mistakenly thought this did not make a difference because I had entered the wrong resource ID.</p> <p><code>MeasureSpec</code> somehow did not give a good account of the width and height. So I ended up using another method:</p> <pre><code>... ScrollView z = (ScrollView) findViewById(R.id.scroll); int totalHeight = z.getChildAt(0).getHeight(); int totalWidth = z.getChildAt(0).getWidth(); u.layout(0, 0, totalWidth, totalHeight); ... </code></pre> <p>As ScrollView's total height can be determined by the single child element that it has.</p> <p>After making these changes, I am now able to take a screenshot of a nested ScrollView and all its contents, visible or not. For anyone interested, here is the block of code including the saving of the bitmap:</p> <pre><code> View u = findViewById(R.id.scroll); u.setDrawingCacheEnabled(true); ScrollView z = (ScrollView) findViewById(R.id.scroll); int totalHeight = z.getChildAt(0).getHeight(); int totalWidth = z.getChildAt(0).getWidth(); u.layout(0, 0, totalWidth, totalHeight); u.buildDrawingCache(true); Bitmap b = Bitmap.createBitmap(u.getDrawingCache()); u.setDrawingCacheEnabled(false); //Save bitmap String extr = Environment.getExternalStorageDirectory().toString() + File.separator + "Folder"; String fileName = new SimpleDateFormat("yyyyMMddhhmm'_report.jpg'").format(new Date()); File myPath = new File(extr, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(myPath); b.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen"); }catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload