问题
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
回答1:
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.
回答2:
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);
回答3:
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 needRun System.gc() to force a garbage collection just before you create the
Bitmap
Hope this helps!
回答4:
Actually you could refactor your code. You'll have better performance and probably use less memory if you don't use a View
- Create a
Bitmap
of the desired sizebitmap = Bitmap.createBitmap(width,height,Bitamp.Config.RGV_565)
(orARGB_8888
, that might work too) - Create a
canvas = new Canvas(bitmap)
- 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)
来源:https://stackoverflow.com/questions/4022860/dealing-with-large-bitmaps-tiling-a-small-bitmap-to-create-wallpaper