IllegalArgumentException: width and height must be > 0 while loading Bitmap from View

I'm trying to draw on an ImageViewTouch, a library which enables pinch zooming. I'm able to draw over the image using Canvas, but when I zoom the image, the drawing disappears.

For this, I'm trying to convert the view to a bitmap and set theImageBitmap for this same view. Here's the code:

mImage.setDrawPath(true);
mImage.setImageBitmap(loadBitmapFromView(mImage)); 

public static Bitmap loadBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap( v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);                
        Canvas c = new Canvas(b);
        v.layout(0, 0, v.getWidth(), v.getHeight());
        v.draw(c);
        return b;
}

When I do this, I get the following log error:

07-11 21:13:41.567: E/AndroidRuntime(20056): java.lang.IllegalArgumentException: width and height must be > 0
07-11 21:13:41.567: E/AndroidRuntime(20056):    at android.graphics.Bitmap.createBitmap(Bitmap.java:638)
07-11 21:13:41.567: E/AndroidRuntime(20056):    at android.graphics.Bitmap.createBitmap(Bitmap.java:620)

If I remove the loadBitmapFromView call the drawing normally appears over the image. When I try to do any interaction with the image (like zooming in or out), the drawing disappears, remaing only the background image, which is a picture.

--- EDIT ---

Here's some more code placed after the loadBitmapFromView call. The case is: I have a radio group listenner and when I check some radio button, I have to load the image and draw some possibles drawings over it.. then I'm trying to convert everything (the image and the drawings) into only one bitmap.

Here's the ohter part of the code:

bitmap = BitmapUtils.decodeSampledBitmapFromResource(root + DefinesAndroid.CAMINHO_SHOPPINGS_SDCARD + nomeImagemAtual, size.x, size.y);
mImage.setImageBitmap(bitmap);

After that, I draw everything I have to draw and try to convert the view to bitmap using the loadImageBitmap method I have shown.

the decodeSampledBitmapFromResource method I got from this link on android developers http://developer.android.com/training/displaying-bitmaps/load-bitmap.html


Solution 1:

I finally figured out a solution for my problem.

I'm using the post method of the View, which will be executed only after the view measuring and layouting, this way getWidth() and getHeight() returns the actual width and height.

Here's the code sample:

mImage.post(new Runnable() {
    @Override
    public void run() {
        mImage.setImageBitmap(loadBitmapFromView(mImage));
    }
});

Hope it also helps someone else :)

Solution 2:

If everything else is correct you are executing your code too early.

The view's layout has not yet been measured by Android. Try executing in OnResume just to see if this is your problem.

Cheers!

Solution 3:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            loadBitmapFromView(mImage);
        }
    }, 1000);

Solution 4:

java.lang.IllegalArgumentException: width and height must be > 0 is because in your Bitmap.createBitmap() call v.getLayoutParams().width or v.getLayoutParams().height is 0. Probably the LayoutParams are wrongly set in mImage.

Update: setLayoutParams() for the mImage before using createBitmap(). Something like this:

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
mImage.setLayoutParams(layoutParams);

Solution 5:

In my case, I forgot to set orientation to vertical for parent LinearLayout and the default value of horizontal has been set, so I set it and my problem gone...

android:orientation="vertical"