Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 798
Felipe Mosso Avatar asked Jul 12 '13 00:07

Felipe Mosso


2 Answers

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 :)

like image 123
Felipe Mosso Avatar answered Oct 09 '22 06:10

Felipe Mosso


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!

like image 27
Sam Aleksov Avatar answered Oct 09 '22 06:10

Sam Aleksov