Dealing with Large Bitmaps (tiling a small bitmap

2019-04-09 21:44发布

I'm having memory problems and think it might have to do with creating large bitmaps.

The task at hand is to get a fairly small tile image and create a larger tiled image and set this as the phone wallpaper. The way I'm doing this is:

1) Create a view that is 2 * screen width, 1 * screen height

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

3) Create a bitmap with the views dimensions

4) draw the view to the bitmap by: view.draw(new Canvas(bitmap))

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

This works fine on my phone (HTC Magic) and other phones that I have tried. But I am getting bug reports relating to this issue. I tried to recreate the problem by doubling the required dimensions and the problem seems to be happening in the 4th step when the view is being drawn to the bitmap:

ERROR/dalvikvm-heap(124): Heap Massage needed (7372800-byte external allocation too big)

I'm not sure how to go about solving this. Please help! Thanks

4条回答
Anthone
2楼-- · 2019-04-09 22:02

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

Here are a couple of tips and tricks I can give you (because I have the sames issues in my application)

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

  • Recycle the image that you don't need when you create your bitmap (and that you won't need to draw your bitmap)

  • null any other object that you don't need

  • Run System.gc() to force a garbage collection just before you create the Bitmap

Hope this helps!

查看更多
来,给爷笑一个
3楼-- · 2019-04-09 22:10

Actually you could refactor your code. You'll have better performance and probably use less memory if you don't use a View

  1. Create a Bitmap of the desired size bitmap = Bitmap.createBitmap(width,height,Bitamp.Config.RGV_565) (or ARGB_8888, that might work too)
  2. Create a canvas = new Canvas(bitmap)
  3. Create the tiled image yourself, 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);
  }
}

Keep the end set wallpaper getApplicationContext().setWallpaper(bitmap)

查看更多
姐就是有狂的资本
4楼-- · 2019-04-09 22:12

Not exactly sure if this is your solution, but have you looked at ? BitmapFactory.Options.inTempStorage

The way you use it is:


BitmapFactory.Options options = new BitmapFactory.Options();
options.inTempStorage = new byte[16*1024];
Bitmap bitmap_origin = BitmapFactory.decodeFile(path, options);
查看更多
Melony?
5楼-- · 2019-04-09 22:13

I'm sure you thought of it, but nevertheless: Have you included

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

in your manifest-file?

You're sure there is no exception thrown? It could possibly be a problem with showing the Toast.

查看更多
登录 后发表回答