0

I want to combine many small bitmaps which are contained in ArrayList to one large bitmap.

However, I don't know why the large bitmap is looped. It means it seems to copy only the first element in the array. I tried to draw each small bitmap in the array to test and it works fine, but when I run the loop like the below code, it goes wrong.

In addiditon, when I add the bmp.recycle() and bmp = null, it causes the error "trying to use a recycled bitmap". I don't understand why the error happens.

Can you help me, please, thanks!

public static Bitmap getBitmapForVisibleRegion(WebView webview) {
        Bitmap returnedBitmap = null;
        webview.setDrawingCacheEnabled(true);
        returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());
        webview.setDrawingCacheEnabled(false);
        return returnedBitmap;
    }


public void CombineBitmap(){
    ArrayList<Bitmap> bmps = new ArrayList<Bitmap>();
    for (int i = 0; i < webView.getWidth; i+=needToCapture){
    bmps.add(getBitmapForVisibleRegion(webView));
    webView.scrollBy(needToCapture, 0);

    }

            Bitmap bigbitmap    = Bitmap.createBitmap(largeBitmapWidth, largeBitmapHeight, Bitmap.Config.ARGB_8888);
            Canvas bigcanvas    = new Canvas(bigbitmap);

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

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

                bmp.recycle();
                bmp=null;
            }
}
lolyoshi
  • 1,536
  • 3
  • 24
  • 42
  • bigcanvas.drawBitmap(bmp, iWidth , 0, paint); Here you are using bmp object to draw in Canvas and after one line you are recycling it bmp.recycle();// that's why its giving you error "trying to use a recycled bitmap". – Kanak Sony Jan 10 '14 at 13:02
  • The only issue I can guess is that you are adding the same Bitmap more than once in your List bmps. – Sherif elKhatib Jan 10 '14 at 13:03
  • @SherifelKhatib: I checked each bitmap of the array and they're totally different. So, why do I get that issue? – lolyoshi Jan 10 '14 at 13:05
  • Show us the code where you populate your `bmps` list. – Sherif elKhatib Jan 10 '14 at 13:06
  • @KanakSony: I read this one http://stackoverflow.com/questions/12742343/android-get-screenshot-of-all-listview-items and try to apply the code to my application, but it causes the error – lolyoshi Jan 10 '14 at 13:08
  • @SherifelKhatib: I updated my code, you can check it – lolyoshi Jan 10 '14 at 13:13

2 Answers2

1

I finally found out my problem. It's because of my dummy mistake.

I have to use scrollTo instead scrollBy

After I change to scrollTo, everything works fine. This is really an useful experience.

lolyoshi
  • 1,536
  • 3
  • 24
  • 42
0

According to the documentation of Bitmap.createBitmap(Bitmap),

Returns an immutable bitmap from the source bitmap. The new bitmap may be the same object as source, or a copy may have been made. It is initialized with the same density as the original bitmap.`

Therefore, replace

returnedBitmap = Bitmap.createBitmap(webview.getDrawingCache());

with

returnedBitmap = webview.getDrawingCache().copy(Config.ARGB_8888, false); 
Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
  • But I even printed out all bitmaps in the array, and I'm sure they're different, so why do I need to replace the code? – lolyoshi Jan 10 '14 at 14:13
  • Because if you are getting the same instance as the source, it is recycled when you do this `webview.setDrawingCacheEnabled(false);`. Therefore, you should ensure that you get a new instance by replacing the code. – Sherif elKhatib Jan 10 '14 at 14:40
  • Ok, I got it. Let me try and tell you the result as soon as possible – lolyoshi Jan 10 '14 at 14:43
  • I know the cause of my error. It's because I'm using scrollBy, when I change to scrollTo, it works fine. Anyway, thank for your help – lolyoshi Jan 11 '14 at 16:29