Work with large bitmaps (tiling a small bitmap to create wallpaper)

I have memory problems and I think that this may be due to the creation of large bitmap images.

The challenge is to get a fairly small tile image and create a larger tiled image and set this as a wallpaper for your phone. The way I do this:

1) Create a view with a screen width of 2 *, a screen height of 1 *

2) Set the view background to BitmapDrawable with the tile mode set to repeat

3) Create a bitmap with view sizes

4) draw the view onto the bitmap using: view.draw (new canvas (bitmap))

5) set wallpper: getApplicationContext (). setWallpaper (bitmap)

This works great on my phone (HTC Magic) and other phones that I have tried. But I get error messages related to this problem. I tried to recreate the problem by doubling the required dimensions, and the problem seems to occur in step 4 when the view accesses the bitmap:

ERROR / dalvikvm-heap (124): Massage with a bunch is required (external placement is too large 7372800 bytes)

I am not sure how to solve this. Please help! Thanks

+4
source share
4 answers

I'm sure you thought about it, but nonetheless: did you turn it on

<uses-permission android:name="android.permission.SET_WALLPAPER" /> 

in the manifest file?

Are you sure there is no exception? Perhaps this was a problem with the Toast show.

+1
source

Not quite sure if this is your decision, but have you looked? BitmapFactory.Options.inTempStorage

Method of use:

 BitmapFactory.Options options = new BitmapFactory.Options(); options.inTempStorage = new byte[16*1024]; Bitmap bitmap_origin = BitmapFactory.decodeFile(path, options); 
+1
source

Unfortunately, I don’t think you can do much ... We are here on a mobile phone; Android limits the process memory to 16 MB.

Here are some tips and tricks I can give you (because I have problems with names in the application)

  • Are you sure you need 32-bit pixels? that three 8-bit color channels plus an 8-bit alpha channel. You can use RGB_565 for a visually acceptable result.

  • Reload the image that you don’t need when you create the bitmap (and you don’t need to draw the bitmap)

  • null any other object you don't need

  • Run System.gc() to force garbage collection just before creating Bitmap

Hope this helps!

0
source

In fact, you can reorganize your code. You will have better performance and probably use less memory if you are not using View

  • Create a Bitmap desired size bitmap = Bitmap.createBitmap(width,height,Bitamp.Config.RGV_565) (or ARGB_8888 , which may work too)
  • Create canvas = new Canvas(bitmap)
  • Create your own tiled image from your src

simplified code:

 // set another matrix if you want rotation/scaling of the input Matrix identity=new Matrix(); for (int i=0; i<maxLines; i++) { for (int j=0; j<maxCol; j++) { canvas.draw(src, identity,anyPaint); } } 

Save the final set of wallpapers getApplicationContext().setWallpaper(bitmap)

0
source

All Articles