12

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

below is my code

    View v1 = mKittyBGLayer.getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap source = v1.getDrawingCache();
    int width = source.getWidth();
    int height = source.getHeight();
    System.out.println("vListView : -"+vListView.getWidth());
    System.out.println("hListView : -"+hListView.getHeight());
    System.out.println("Width : -"+width);
    System.out.println("Height : -"+height);
    bitmap = Bitmap.createBitmap(source, vListView.getWidth(), 0, width, height - hListView.getHeight());

my logcat is

        11-01 11:00:31.419: I/System.out(1658): vListView :- 60
        11-01 11:00:31.429: I/System.out(1658): hListView :- 60
        11-01 11:00:31.429: I/System.out(1658): Width :- 480
        11-01 11:00:31.429: I/System.out(1658): Height :- 320
        11-01 11:00:31.429: D/AndroidRuntime(1658): Shutting down VM
        11-01 11:00:31.429: W/dalvikvm(1658): threadid=1: thread exiting with uncaught exception  (group=0x40018560)
        11-01 11:00:31.429: E/AndroidRuntime(1658): FATAL EXCEPTION: main
        11-01 11:00:31.429: E/AndroidRuntime(1658): java.lang.IllegalArgumentException: x + width  must be <= bitmap.width()
        11-01 11:00:31.429: E/AndroidRuntime(1658):     at android.graphics.Bitmap.createBitmap(Bitmap.java:410)
        11-01 11:00:31.429: E/AndroidRuntime(1658):     at android.graphics.Bitmap.createBitmap(Bitmap.java:383)
        11-01 11:00:31.429: E/AndroidRuntime(1658):     at com.appsehs.android.CUTECRAZYKITTENDRESSUPGAME.PhotoSortrActivity.takeScreenShot(PhotoSortrActivity.java:247)
        11-01 11:00:31.429: E/AndroidRuntime(1658):     at com.appsehs.android.CUTECRAZYKITTENDRESSUPGAME.PhotoSortrActivity.onOptionsItemSelected(PhotoSortrActivity.java:274)
        11-01 11:00:31.429: E/AndroidRuntime(1658):     at android.app.Activity.onMenuItemSelected(Activity.java:2205)

Here you can see that x < bitmap.getWidth mean 60 < 480

although i am getting error

4
  • on which statement u r getting the error? Commented Nov 1, 2012 at 5:37
  • bitmap = Bitmap.createBitmap(source, vListView.getWidth(), 0, width, height - hListView.getHeight()); Commented Nov 1, 2012 at 5:41
  • if i do bitmap = Bitmap.createBitmap(source, 0, 0, width, height - hListView.getHeight()); it will work Commented Nov 1, 2012 at 5:42
  • ok @Siddhpura ur problem is that ur vListView.getWidth() gives the width that is more than the width of ur bitmap so it throws the error. get my point. Commented Nov 1, 2012 at 5:50

4 Answers 4

25

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());
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Eric, can you tell me how did you get this? i have read doc but can not find this! Eric i want to crop image from left side! please can you tell me how can i do this?
I am having an issue with my code: stackoverflow.com/questions/21347431/…
1
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.

Comments

0

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

I have same issue. Now, working perfactly.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.