createBitmap — java.lang.IllegalArgumentException: x must be < bitmap.width()

前端 未结 4 1821
天涯浪人
天涯浪人 2020-12-09 11:50

I am getting error while taking screenshot and create bitmap with cropping picture

below is my code

    View v1 = mKittyBGLayer.getRootView();
    v1         


        
相关标签:
4条回答
  • 2020-12-09 12:21
    Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
            sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);
    

    Like in lists, it starts from 0, so width must be bitmap width - 1.

    0 讨论(0)
  • 2020-12-09 12:30

    If you are get image from drawable then add it into drawable-nodpi folder.

    I have same issue. Now, working perfactly.

    0 讨论(0)
  • 2020-12-09 12:40

    No, not x must be < bitmap.width(). It says x + width must be <= bitmap.width().

    You are creating a Bitmap like so:

    Bitmap.createBitmap(source, 60, 0, 480, 260); // 320 - 60 = 260
    

    Basically, you are drawing from x = 60, y = 0 to x = 480 + 60, y = 260 on a Bitmap which is only 480x320. Obviously, this is not possible, since your x coordinate is off the Bitmap.

    It's hard to tell you how to fix this without knowing your exact use case. Basically, your source image has to fit within { x1: x, x2: x + width, y1: y, y2: y + height }.

    If you only want to draw from the 60th pixel onward, then you need to do this:

    Bitmap.createBitmap(source, vListView.getWidth(), 0, width - vListView.getWidth(), height - hListView.getHeight());
    
    0 讨论(0)
  • 2020-12-09 12:41

    If you are getting java.lang.IllegalArgumentException: x must be < bitmap.width()

    This is system crash on version 6.0.1 on Samsungs. At this moment I couldn't find cause.

    0 讨论(0)
提交回复
热议问题