edit:
Nevermind, got it working that way
TopRatedPage.setDrawingCacheEnabled(true);
TopRatedPage.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
TopRatedPage.layout(0, 0, TopRatedPage.getMeasuredWidth(), TopRatedPage.getMeasuredHeight());
TopRatedPage.buildDrawingCache(true);
Bitmap screenshot = Bitmap.createBitmap(TopRatedPage.getDrawingCache());
TopRatedPage.setDrawingCacheEnabled(false);
old:
I'm trying to take a screenshot from a layout, like so.
LinearLayout TopRatedPage = (LinearLayout)inflater.inflate(R.layout.toprated, null);
...
Bitmap screenshot;
TopRatedPage.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(TopRatedPage.getDrawingCache()); // Caused by: java.lang.NullPointerException
TopRatedPage.setDrawingCacheEnabled(false);
Any ideas what I have done wrong?
Thanks!
edit: also tried like this, it doesn't throw an error, but given an empty bitmap.
Bitmap screenshot = TopRatedPage.getDrawingCache();
Try this and let me know what happen..
LinearLayout TopRatedPage = (LinearLayout)inflater.inflate(R.layout.toprated, null);
Bitmap screenshot = Bitmap.createBitmap(LinearLayout .getWidth(), LinearLayout .getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
LinearLayout.draw(c);
imageView.setImageBitmap(screenshot);
Also if possible, use,
View captureView = null;
captureView = inflater.inflate(R.layout.toprated, null);
Bitmap screenshot = Bitmap.createBitmap(captureView.getWidth(), captureView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(screenshot);
captureView .draw(c);
imageView.setImageBitmap(screenshot);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With