-4

I recently created my own Android live wallpaper project with 18 images but it can't set as wallpaper. If I go to set it as wallpaper I see following error message. Unfortunately Example has stop Working. Image resolution 480x800. I used 18 images. Project size is 2.67 MB. But I can't find any error in my project. Anyone know what is this error?

public class star extends WallpaperService {

    public void onCreate(){
        super.onCreate();
    }

    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public Engine onCreateEngine() {
        return new WallpaperEngine();
    }

    private class WallpaperEngine extends Engine{

        Bitmap i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i21,i22,i23,i24,i25,i26,i27,i28,i29,i30;
        int position;
        int screenwidth;
        DisplayMetrics metric;
        int count=0;

        private final Handler hanlder=new Handler();
        private boolean visible=false;
        private final Runnable r=new Runnable() {

            @Override
            public void run() {
                 draw();
            }
        };

        WallpaperEngine() {
            i1=BitmapFactory.decodeResource(getResources(), R.drawable.image1);
            i2=BitmapFactory.decodeResource(getResources(), R.drawable.image2);
            i3=BitmapFactory.decodeResource(getResources(), R.drawable.image3);
            i4=BitmapFactory.decodeResource(getResources(), R.drawable.image4);
            i5=BitmapFactory.decodeResource(getResources(), R.drawable.image5);
            i6=BitmapFactory.decodeResource(getResources(), R.drawable.image6);
            i7=BitmapFactory.decodeResource(getResources(), R.drawable.image7);
            i8=BitmapFactory.decodeResource(getResources(), R.drawable.image8);
            i9=BitmapFactory.decodeResource(getResources(), R.drawable.image9);
            i10=BitmapFactory.decodeResource(getResources(), R.drawable.image10);
            i11=BitmapFactory.decodeResource(getResources(), R.drawable.image11);
            i12=BitmapFactory.decodeResource(getResources(), R.drawable.image12);
            i13=BitmapFactory.decodeResource(getResources(), R.drawable.image13);
            i14=BitmapFactory.decodeResource(getResources(), R.drawable.image14);
            i15=BitmapFactory.decodeResource(getResources(), R.drawable.image15);
            i16=BitmapFactory.decodeResource(getResources(), R.drawable.image16);
            i17=BitmapFactory.decodeResource(getResources(), R.drawable.image17);
            i18=BitmapFactory.decodeResource(getResources(), R.drawable.image18);
            i19=BitmapFactory.decodeResource(getResources(), R.drawable.image19);
            i20=BitmapFactory.decodeResource(getResources(), R.drawable.image20);   
            position=0;
            metric=getBaseContext().getResources().getDisplayMetrics();
            screenwidth= metric.widthPixels;
        }

        public void onCreate(SurfaceHolder holder){
            super.onCreate(holder);
        }

        public void onVisibilityChanged(boolean visible){
            this.visible=visible;
            if (visible) {
                hanlder.post(r);
            } else {
                hanlder.removeCallbacks(r);
            }
        }

        public void onSurfaceDestroyed(SurfaceHolder holder){
            visible=false;
            hanlder.removeCallbacks(r);
        }

        public void draw(){

            Bitmap arr[]={i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12,i13,i14,i15,i16,i17,i18,i19,i20,i19,i18,i17,i16,i15,i14,i13,i12,i11,i10,i9,i8,i7,i6,i5,i4,i3,i2};
            SurfaceHolder holder=getSurfaceHolder();
            Canvas c=null;

            try {

                c=holder.lockCanvas();
                if(c!=null){
                    c.drawColor(Color.WHITE);
                    c.drawBitmap(arr[count], position,0, null);
                    ++count;

                    if(count==arr.length){
                        count=0;
                    }
                }

            } finally {
                if(c!=null){
                    holder.unlockCanvasAndPost(c);
                }
            }

            hanlder.removeCallbacks(r);
            if(visible){
                hanlder.postDelayed(r, 70);
            }

        }
    }
}

After I received following LOGCAT error:

09-26 14:07:01.005: E/AndroidRuntime(2767): FATAL EXCEPTION: main
09-26 14:07:01.005: E/AndroidRuntime(2767): java.lang.OutOfMemoryError
09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:378)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:408)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.example.stargate.star$WallpaperEngine.<init>(star.java:55)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.example.stargate.star.onCreateEngine(star.java:27)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.service.wallpaper.WallpaperService$IWallpaperEngineWrapper.executeMessage(WallpaperService.java:1034)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.android.internal.os.HandlerCaller$MyHandler.handleMessage(HandlerCaller.java:40)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.os.Handler.dispatchMessage(Handler.java:99)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.os.Looper.loop(Looper.java:137)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at android.app.ActivityThread.main(ActivityThread.java:5041)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at java.lang.reflect.Method.invokeNative(Native Method)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at java.lang.reflect.Method.invoke(Method.java:511)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

09-26 14:07:01.005: E/AndroidRuntime(2767):     at dalvik.system.NativeStart.main(Native Method)

09-26 14:08:11.935: E/Trace(4999): error opening trace file: No such file or directory (2)
Falko
  • 17,076
  • 13
  • 60
  • 105
  • 3
    If you're getting an error message, your code probably isn't 100% correct. – nhgrif Oct 04 '13 at 16:04
  • 3
    If you're getting an error message, you should post it. Logcat, please. – Geobits Oct 04 '13 at 16:05
  • 1
    You have an uncaught exception somewhere. Run the wallpaper with your device attached and look for errors in logcat. *Edit* Rather look for the big long stack trace that will be resulting from the error. – Erik Nedwidek Oct 04 '13 at 16:05
  • 5
    Probably OOM. `480 * 800 * 4bpp * 18images = 27MB` – Geobits Oct 04 '13 at 16:06

2 Answers2

1

Why are you drawing 18 bitmaps on top of one another? You're running out of memory! Only decode and load them up when you need them!!!

Trim that code down to 3 bitmaps and see if the error still occurs, I bet it won't.

Falko
  • 17,076
  • 13
  • 60
  • 105
MobileMon
  • 8,341
  • 5
  • 56
  • 75
0

You can use this code.

class Diwali extends Engine  {

    private boolean mVisible;

    private final Runnable diwaliImg = new Runnable() {
        public void run() {
            drawFrame();
        }
    };

    int i=0;
    int[] pirates = {
        R.drawable.a1, R.drawable.a2,
        R.drawable.a3, R.drawable.a4,
        R.drawable.a5, R.drawable.a6,
        R.drawable.a7, R.drawable.a8,
        R.drawable.a9, R.drawable.a10,
        R.drawable.a11, R.drawable.a12,
        R.drawable.a13, R.drawable.a14
    };

    @Override
    public void onCreate(SurfaceHolder holder){
        super.onCreate(holder);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacks(diwaliImg);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        mVisible = visible;
        if (visible) {
            drawFrame();
        } else {
            mHandler.removeCallbacks(diwaliImg);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
        drawFrame();    
    }

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceCreated(holder);
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceDestroyed(holder);
        mVisible = false;
        mHandler.removeCallbacks(diwaliImg);
    }


    @Override
    public void onOffsetsChanged(float xOffset, float yOffset, float xStep,float yStep, int xPixels, int yPixels) {
        drawFrame();
    }

    @Override
    public void onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
    }

    private void drawFrame() {
        // TODO Auto-generated method stub
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                drawPirate(c);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
        mHandler.removeCallbacks(diwaliImg);
        if (mVisible) {
            mHandler.postDelayed(diwaliImg,60);
        }
    }

    private void drawPirate(Canvas c) {
        // TODO Auto-generated method stub
        Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]);

        i++;

        if (i == 13) {
            i = 0;
        }
        Matrix matrix = new Matrix();

        c.drawBitmap(icon, matrix, null);
        icon.recycle();
    }
}
Falko
  • 17,076
  • 13
  • 60
  • 105
jam
  • 11
  • 5