user2586419
user2586419

Reputation:

y + height must be <= bitmap.height() in android

I want to create a bitmap form another but for each try it's a crash, this the error message

java.lang.IllegalArgumentException: y + height must be <= bitmap.height() at android.graphics.Bitmap.createBitmap(Bitmap.java:557) at android.graphics.Bitmap.createBitmap(Bitmap.java:522) saveBitmap(GesturesActivity.java:166)

This is where i try the creation :

public void saveBitmap(){
    Bitmap bitmapToSave = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmapToSave);
    view.draw(canvas);

    int width = bitmapToSave.getWidth();
    int height = bitmapToSave.getHeight();
    System.out.println("view : "+view.getWidth());
    System.out.println("view : "+view.getHeight());
    System.out.println("btmp Width : "+width);
    System.out.println("btmp Height : "+height);

    Bitmap result = Bitmap.createBitmap(bitmapToSave, 0, view.getTop(), view.getWidth(), view.getWidth());
    SaveImage(result);
}

Thanks for your help :D

This is my logcat :

> System.out﹕ view : 480
System.out﹕ view : 480
System.out﹕ btmp Width : 480
System.out﹕ btmp Height : 480

Upvotes: 5

Views: 12729

Answers (2)

San
San

Reputation: 5697

Try

Bitmap result = Bitmap.createBitmap(bitmapToSave, 0, 0, view.getWidth(), view.getHeight());

I think the issue is view.getTop is a value greater than 0. So view.GetTop + view.getHeight() is not less than bitmap.height.

Upvotes: 3

Neil Townsend
Neil Townsend

Reputation: 6084

In your code, in the second BitmapCreate:

Bitmap result = Bitmap.createBitmap(bitmapToSave, 0, view.getTop(), view.getWidth(), view.getWidth());

The height argument (the last one) is set to be View.getWidth(). However, nowhere in the code have you checked that the width of the view, added to the Top of the view, is not greater than the height of bitmapToSave. A check to this effect should go in.

Upvotes: 1

Related Questions