0

I have a service using which i am changing android wallpaper. I am passing path of images to the service and then using following code to set the image as wallpaper:

@Override
public void run() {
    try {
        final String imagePath[] = mSelectedImgPath.split(",");
        while (true) {
            for (int i = 0; i < imagePath.length; i++) {
                bitmap = BitmapFactory.decodeFile(imagePath[i]);
                this.setWallpaper(bitmap);
                Thread.sleep(1000 * time);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

I am getting following error:

01-25 15:59:12.410: E/AndroidRuntime(1702): FATAL EXCEPTION: Thread-149
    01-25 15:59:12.410: E/AndroidRuntime(1702): java.lang.OutOfMemoryError
    01-25 15:59:12.410: E/AndroidRuntime(1702):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    01-25 15:59:12.410: E/AndroidRuntime(1702):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:493)
    01-25 15:59:12.410: E/AndroidRuntime(1702):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:299)
    01-25 15:59:12.410: E/AndroidRuntime(1702):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:324)
    01-25 15:59:12.410: E/AndroidRuntime(1702):     at com.wallpaper.demo.WallpaperChangeService.run(WallpaperChangeService.java:55)
    01-25 15:59:12.410: E/AndroidRuntime(1702):     at java.lang.Thread.run(Thread.java:856)

Thanks

Veger
  • 37,240
  • 11
  • 105
  • 116
Ashwani Kumar
  • 834
  • 3
  • 16
  • 30
  • Try to resize your bitmap image and set as wallpaper. – GrIsHu Jan 25 '13 at 10:36
  • I am resizing the images before setting them as wallpaper as per the device screen size. I thing the issue is related to something else. – Ashwani Kumar Jan 25 '13 at 10:37
  • I don't think so. the Log is clearly telling you, that you ran out-of-memory by trying to decode the Bitmap from its path. You should try to decode it with bounds, or maybe giving BitmapOptions a compression level lower than 100 – Rafael T Jan 25 '13 at 10:43
  • have a look at : http://stackoverflow.com/a/14117007/1070711 , hope this will give you some hint about your problem. – Moin Ahmed Jan 25 '13 at 10:47

3 Answers3

0

Increase your "VM heap" if your running program in emulator. Goto avd manager and click on to ur android emulator and increase size VM heap.

duggu
  • 37,851
  • 12
  • 116
  • 113
0

Its because your images might be in big size, try to scale your images.

refer this Strange out of memory issue while loading an image to a Bitmap object

Community
  • 1
  • 1
Mac
  • 1,153
  • 2
  • 20
  • 34
0

Use

bitmap.recycle();

just after entering the loop

  for (int i = 0; i < imagePath.length; i++) {
            bitmap.recycle();
            bitmap = BitmapFactory.decodeFile(imagePath[i]);
            this.setWallpaper(bitmap);
            Thread.sleep(1000 * time);
        }
Vineet
  • 151
  • 2
  • 10