22
View v = rootView.findViewById(R.id.layout1);
if (v != null) {
    v.buildDrawingCache();
    Bitmap bitmap = v.getDrawingCache();
    canvas.drawBitmap(bitmap, dummyMatrix, null);
    v.destroyDrawingCache();   
}

I have this code. But I need to screenshot all my ListView items, but if my listview's have more items than visible on the screen, this code don't capture when the items bigger than the visible rect.

How to capture my ListView correctly?

NEW WORKING CODE CREATED BY ME

public static Bitmap getWholeListViewItemsToBitmap() {

    ListView listview    = MyActivity.mFocusedListView;
    ListAdapter adapter  = listview.getAdapter(); 
    int itemscount       = adapter.getCount();
    int allitemsheight   = 0;
    List<Bitmap> bmps    = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView      = adapter.getView(i, null, listview);
        childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight+=childView.getMeasuredHeight();
    }

    Bitmap bigbitmap    = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
    Canvas bigcanvas    = new Canvas(bigbitmap);

    Paint paint = new Paint();
    int iHeight = 0;

    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight+=bmp.getHeight();

        bmp.recycle();
        bmp=null;
    }


    return bigbitmap;
}
lacas
  • 13,928
  • 30
  • 109
  • 183
  • 1
    waiting for the answer to this question. – G_S Oct 05 '12 at 08:26
  • @Sharath check my updated code: getWholeListViewItemsToBitmap() function – lacas Oct 05 '12 at 18:35
  • The code just gave a blank back screen as output – G_S Oct 05 '12 at 19:06
  • My code is working on my custom listview/listadapter on my samsung galaxy s2. Do u tried in a custom listview/adapter? – lacas Oct 05 '12 at 19:17
  • no am working for a simple listview. – G_S Oct 05 '12 at 19:19
  • yes the code is working to get a screen shot of listview but the background remains a black one even though i used a white background for the listview. Can you suggest me lacas – G_S Oct 05 '12 at 19:28
  • yeah I dont know why, maybe the FILL_PARENT, MATCH_PARENT and textview LayoutParams, etc parameters not running down before drawing it to the cache... – lacas Oct 05 '12 at 19:40
  • @Sharath check out my new working code "NEW WORKING CODE CREATED BY ME" it is now working!!! – lacas Oct 07 '12 at 14:47
  • 2
    Am sorry to say this lacas. It didnt work for me yet. :( . I am getting the black background where the background of my sample is white. :( – G_S Oct 07 '12 at 16:53
  • maybe your Views background is transparent – lacas Oct 08 '12 at 14:03
  • Hey I have tried your code with my listview but its throwing an error like: 04-23 09:06:16.224: E/AndroidRuntime(4928): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListView‌​}: java.lang.IllegalArgumentException: width and height must be > 0 Can you give me any solution about this error? – GrIsHu Apr 23 '15 at 13:10

4 Answers4

47

working code:

public static Bitmap getWholeListViewItemsToBitmap() {

    ListView listview    = MyActivity.mFocusedListView;
    ListAdapter adapter  = listview.getAdapter(); 
    int itemscount       = adapter.getCount();
    int allitemsheight   = 0;
    List<Bitmap> bmps    = new ArrayList<Bitmap>();

    for (int i = 0; i < itemscount; i++) {

        View childView      = adapter.getView(i, null, listview);
        childView.measure(MeasureSpec.makeMeasureSpec(listview.getWidth(), MeasureSpec.EXACTLY), 
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));

        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
        childView.setDrawingCacheEnabled(true);
        childView.buildDrawingCache();
        bmps.add(childView.getDrawingCache());
        allitemsheight+=childView.getMeasuredHeight();
    }

    Bitmap bigbitmap    = Bitmap.createBitmap(listview.getMeasuredWidth(), allitemsheight, Bitmap.Config.ARGB_8888);
    Canvas bigcanvas    = new Canvas(bigbitmap);

    Paint paint = new Paint();
    int iHeight = 0;

    for (int i = 0; i < bmps.size(); i++) {
        Bitmap bmp = bmps.get(i);
        bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
        iHeight+=bmp.getHeight();

        bmp.recycle();
        bmp=null;
    }


    return bigbitmap;
}
lacas
  • 13,928
  • 30
  • 109
  • 183
  • Hello there, I tried to implement your code to my application, but I got issues. One is the large bitmap just draw only first element in the array, the other is I got the "trying to using recycle bitmap" when using bmp.recycle(). Can you help me? For detail: http://stackoverflow.com/questions/21044984/android-combine-many-bitmaps-to-one-large-one-goes-wrong-and-cant-recycle-bitma?noredirect=1#comment31640898_21044984 – lolyoshi Jan 10 '14 at 14:01
  • Here MyActivity.mFocusedListView; what it mFocusedListview ?? –  Feb 05 '14 at 01:43
  • can we use same thing on gridview? i have tried but its give me nullpointer at bigcanvas.drawBitmap(bmp, 0, iHeight, paint); any idea? – Dhaval Parmar Feb 08 '14 at 07:03
  • @lolyoshi your issue "trying to using recycle bitmap" can be resolved by commenting bmp.recycle, As android not allows to reuse recycled bitmap – Kimmi Dhingra Sep 23 '14 at 10:05
  • Nice. Ported this to Xamarin C# and it works a treat – ED-209 Dec 18 '14 at 16:17
  • Hey @lacas I have tried your code with my listview but its throwing an error like: 04-23 09:06:16.224: E/AndroidRuntime(4928): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidhive/com.example.androidhive.CustomizedListView}: java.lang.IllegalArgumentException: width and height must be > 0 Can you give me any solution about this error? – GrIsHu Apr 23 '15 at 13:09
  • Thanks... Nice Answer – Santhosh Mar 28 '20 at 13:32
  • this code is working but there is some clarity loss. is there any way to avoid that? – MarGin Apr 27 '21 at 13:24
  • If there are dynamic images in the recyclerview(images load from backend server), the screenshot will not caputure the images(show a black background), any idea to avoid this? – zdd Dec 27 '21 at 06:48
6

While it is impossible to make a screenshot of not-yet-rendered content (like off-screen items of the ListView), you can make a multiple screenshots, scroll content between each shot, then join images. Here is a tool which can automate this for you: https://github.com/PGSSoft/scrollscreenshot

illustration

Disclaimer: I'm author of this tool, it was published by my employer. Feature requests are welcome.

tomash
  • 12,742
  • 15
  • 64
  • 81
0

Use this function to get bitmap of your list view

public Bitmap getBitmapFromView(View view) {

Bitmap returnedBitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
              view.getMeasuredHeight() , Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(returnedBitmap);
Drawable bgDrawable = view.getBackground();
if (bgDrawable != null)
    bgDrawable.draw(canvas);
else
    canvas.drawColor(Color.WHITE);
view.draw(canvas);
        return returnedBitmap;
}

use this as

Bitmap b = getBitmapFromView(your listview object here);

and use this bitmap as you want

hope help..

Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
  • not working, i am working with fragments, one fragment - one listview. this code is the same than the others... – lacas Oct 05 '12 at 14:34
  • 1
    this code is not working when list has a scroll (more elements that that are visible). – G_S Oct 05 '12 at 18:45
0

if your generated bitmap is having black color background . this is because you view unable to fetch color from xml file. so set a color of your view

View v = adapter.getView(i, null, listview);
            v.measure(View.MeasureSpec.makeMeasureSpec(listview.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
            v.setDrawingCacheEnabled(true);
            v.buildDrawingCache(true);
            v.setBackgroundColor(Color.parseColor("#F08080"));
            bmps.add(v.getDrawingCache(true));
            allitemsheight += v.getMeasuredHeight();
Akash Kumar
  • 649
  • 6
  • 12