-1

I use this code to scale a bitmap in android, But doesn't work and App stops. I attached LOG CAT log please say what's problem!?

screen=Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),screenID),gameSurface.width,gameSurface.height,false);

Log Cat::

    07-11 19:13:19.878: E/AndroidRuntime(22770): FATAL EXCEPTION: main
07-11 19:13:19.878: E/AndroidRuntime(22770): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.game/com.example.game.GameScreen}: java.lang.IllegalArgumentException: width and height must be > 0
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.os.Looper.loop(Looper.java:137)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.main(ActivityThread.java:4441)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at java.lang.reflect.Method.invoke(Method.java:511)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at dalvik.system.NativeStart.main(Native Method)
07-11 19:13:19.878: E/AndroidRuntime(22770): Caused by: java.lang.IllegalArgumentException: width and height must be > 0
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.graphics.Bitmap.createBitmap(Bitmap.java:603)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:437)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.example.game.Map1.<init>(Map1.java:21)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.example.game.gameSurface.<init>(gameSurface.java:52)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at com.example.game.GameScreen.onCreate(GameScreen.java:37)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.Activity.performCreate(Activity.java:4465)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-11 19:13:19.878: E/AndroidRuntime(22770):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
07-11 19:13:19.878: E/AndroidRuntime(22770):    ... 11 more
  • possible duplicate of [Strange out of memory issue while loading an image to a Bitmap object](http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object) – tyczj Jul 11 '14 at 15:19
  • instead of using gameSurface.width,gameSurface.height try setting absolute numbers and see if you still get error. – etr Jul 11 '14 at 15:24
  • width and height must be > 0 – pskink Jul 11 '14 at 15:25
  • Ok, Works with real numbers. How Can I use game.width? should I define It final? –  Jul 11 '14 at 15:26
  • I have static field in other class that i use them as width and height –  Jul 11 '14 at 15:27
  • createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter) , make sure its type int and larger than 0. – etr Jul 11 '14 at 15:29
  • Can you show us how you derive your width & height? I ran into this problem sometimes when doing math between datatypes (especially float) – Grambot Jul 11 '14 at 15:30

3 Answers3

0

In your LOG CAT you can see :

Caused by: java.lang.IllegalArgumentException: width and height must be > 0

So you have probably set gameSurface.width or gameSurface.height to 0.

AlonsoFloo
  • 505
  • 3
  • 10
  • I have static field in other class that i use them as width and height –  Jul 11 '14 at 15:28
0

try using this in your onCreate

Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);

        screenWidth = size.x;
        screenHeight = size.y;

and then use those for your width and height.. my guess is that at the time you are calling the width and height those values are 0 because they are not done drawing or something

erik
  • 4,946
  • 13
  • 70
  • 120
  • I have this code in other class and I gave the screenWidth and screenHeight to two static varuables and use them here –  Jul 11 '14 at 15:30
  • I store screen width and height in gameSurface class as static field. Then I use static fileds in screen=Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(),screenID),gameSurface.width,gameSurface.height,false); –  Jul 11 '14 at 15:33
0
public static Bitmap  createScaledBitmap (Bitmap src, int dstWidth, int dstHeight, boolean filter) 

You are getting a IllegalArgumentException because your width is <= 0, or height is <= 0.

etr
  • 1,252
  • 2
  • 8
  • 15
  • so How can I use them? cuase I can use those static field in other places but I use them for this In constructorr of class –  Jul 11 '14 at 15:38