-3

I can´t draw bitmap from variable. My application crashes, when I star it.

public class krou extends View {


    public static int Width = aktivita.width;
    public static int Height = aktivita.height;



    final Paint mMalovani;
    public static Bitmap mBitmap;
    public static Bitmap rc;


    public void draw(Canvas canvas) {

        Paint p = new Paint();

        p.setColor(Color.RED);

        int ods = (Height - Width) / 2;



        Bitmap imageOne = BitmapFactory.decodeResource(getResources(), R.drawable.ri50_z);
        Bitmap ImageTwo = BitmapFactory.decodeResource(getResources(), R.drawable.ri50);

        rc = ImageTwo;



        Bitmap scaledBitmap = Bitmap.createScaledBitmap(rc, Width, Width, true);

        Rect dstRectForRender = new Rect(0, odstup, Width, Width + ods);
        canvas.drawBitmap(scaledBitmap, null, dstRectForRender, null);

If I don't use rc variable, and I simple use imageOne or ImageTwo, everything is Ok.

Thanks for your advice.

My LogCat:

09-01 19:29:16.197: E/dalvikvm-heap(1472): Out of memory on a 5760016-byte allocation.
09-01 19:29:16.197: I/dalvikvm(1472): "main" prio=5 tid=1 RUNNABLE
09-01 19:29:16.197: I/dalvikvm(1472):   | group="main" sCount=0 dsCount=0 obj=0x40a729a0 self=0x2a00bba8
09-01 19:29:16.197: I/dalvikvm(1472):   | sysTid=1472 nice=0 sched=0/0 cgrp=apps handle=1073849308
09-01 19:29:16.207: I/dalvikvm(1472):   | state=R schedstat=( 3712781811 1124880434 230 ) utm=328 stm=43 core=0
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-01 19:29:16.207: I/dalvikvm(1472):   at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-01 19:29:16.207: I/dalvikvm(1472):   at org.vkedco.mobappdev.draw_touch_drive_00001.krouzky.draw(krouzky.java:71)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13596)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13715)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13596)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.217: I/dalvikvm(1472):   at android.view.View.draw(View.java:13594)
09-01 19:29:16.227: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.227: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.View.draw(View.java:13594)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.View.draw(View.java:13715)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-01 19:29:16.237: I/dalvikvm(1472):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
09-01 19:29:16.237: I/dalvikvm(1472):   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.os.Looper.loop(Looper.java:137)
09-01 19:29:16.248: I/dalvikvm(1472):   at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:29:16.248: I/dalvikvm(1472):   at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:29:16.248: I/dalvikvm(1472):   at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:29:16.248: I/dalvikvm(1472):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:29:16.248: I/dalvikvm(1472):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:29:16.248: I/dalvikvm(1472):   at dalvik.system.NativeStart.main(Native Method)
09-01 19:29:16.267: D/skia(1472): --- allocation failed for scaled bitmap
09-01 19:29:16.267: D/AndroidRuntime(1472): Shutting down VM
09-01 19:29:16.298: W/dalvikvm(1472): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
09-01 19:29:16.348: E/AndroidRuntime(1472): FATAL EXCEPTION: main
09-01 19:29:16.348: E/AndroidRuntime(1472): java.lang.OutOfMemoryError
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at org.vkedco.mobappdev.draw_touch_drive_00001.krouzky.draw(krouzky.java:71)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13596)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13715)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13596)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13594)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13594)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.drawChild(ViewGroup.java:2928)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2797)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.View.draw(View.java:13715)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.widget.FrameLayout.draw(FrameLayout.java:467)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2211)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2281)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.draw(ViewRootImpl.java:2177)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2045)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Handler.handleCallback(Handler.java:725)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.os.Looper.loop(Looper.java:137)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invokeNative(Native Method)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at java.lang.reflect.Method.invoke(Method.java:511)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-01 19:29:16.348: E/AndroidRuntime(1472):     at dalvik.system.NativeStart.main(Native Method)
09-01 19:29:24.396: I/Process(1472): Sending signal. PID: 1472 SIG: 9
Simon
  • 14,407
  • 8
  • 46
  • 61
Josef Josef
  • 65
  • 2
  • 9
  • 1
    What is the question? – BackSlash Sep 01 '13 at 18:49
  • Where is a problem, why my application get down, If I use rc variable. – Josef Josef Sep 01 '13 at 18:57
  • Please describe "*gets down*". Do you get any errors? What is `ImageTwo`: class or object? Where is it defined? – PM 77-1 Sep 01 '13 at 19:08
  • Sorry I mean shut down. I have only LogCat record, because eclipse doesn't show any errors. – Josef Josef Sep 01 '13 at 19:12
  • `I have only logcat record`. You mean, the most important and useful way of figuring out why an app crashes? It might be useful to show us! Eclipse won't show you any errors, otherwise, you would not be able to run the app at all. – Simon Sep 01 '13 at 19:23
  • Please search on here, and on Google, for Android bitmap out of memory. There are many solutions to this. – Simon Sep 01 '13 at 19:41
  • My problem is resolved, Im noob. So my resolution is declare variable Bitmap rc; in draw method. Sorry for wasting with your time. – Josef Josef Sep 01 '13 at 20:13

1 Answers1

0

As I answered previously - Out of memory while creating bitmaps on device

You are getting OutOfMemoryerror because your bitmaps are very heavy They are over 57MB Scale your all your Bitmaps by this method-

  Bitmap imageOne = decodeSampledBitmapFromResource(getResources(), R.drawable.ri50_z,height,width);//might be your screen hight and width

Implemantion of method is-

      public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
    int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}


 public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

    // Calculate ratios of height and width to requested height and width
    final int heightRatio = Math.round((float) height / (float) reqHeight);
    final int widthRatio = Math.round((float) width / (float) reqWidth);

    // Choose the smallest ratio as inSampleSize value, this will guarantee
    // a final image with both dimensions larger than or equal to the
    // requested height and width.
    inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}

return inSampleSize;
}

and also scale your bitmap src refer above link-

This will solve your problem.

Community
  • 1
  • 1
T_V
  • 17,440
  • 6
  • 36
  • 48