Android的例外 - 外部分配太大,这一过程 [英] Android exception - External allocation too large for this process
本文介绍了Android的例外 - 外部分配太大,这一过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下code:
私人位图mBitmap;
私人帆布mCanvas;
私人路径的mpath;
私人涂料mBitmapPaint;
mBitmap = Bitmap.createBitmap(480,800,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);保护无效的onDraw(帆布油画){canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(的mpath,mRealPaint);
}
当我运行它,它运行罚款几页。几张照片完成后,然后就强制关闭给以下异常:
E / GraphicsJNI(493):虚拟机不会让我们分配921600字节
D / AndroidRuntime(493):关闭VM
W / dalvikvm(493):主题ID = 1:螺纹未捕获的异常退出(组= 0x4001d800)
E / AndroidRuntime(493):致命异常:主要
E / AndroidRuntime(493):java.lang.OutOfMemoryError:位图大小超过VM预算
我看了关于这一主题的许多职位。更新的大型堆为真,但它仍然没有工作。有人可以帮我告诉我究竟做错了什么?
更多的错误:
W / WebCore的(541):第一个布局后无法获得viewWidth
D / webviewglue(541):nativeDestroy观点:0x40a7b8
I /广告(541):onReceiveAd()
D / dalvikvm(541):GC_EXTERNAL_ALLOC 1943年释放的对象/在227ms 219448字节
D / dalvikvm(541):GC_EXTERNAL_ALLOC释放707对象/在159ms 100576字节
E / ActivityThread(541):未能找到com.google.plus.platform供应商信息
解决方案
请尝试以下code: -
公共静态位图createScaledBitmap(位图SRC,诠释dstWidth,
INT dstHeight,布尔型过滤器){
矩阵m;
同步(Bitmap.class){
仅1矩阵//小水池
M = sScaleMatrix;
sScaleMatrix = NULL;
} 如果(M == NULL){
M =新的Matrix();
} 最终诠释宽度= src.getWidth();
最终诠释身高= src.getHeight();
最终浮动SX = dstWidth /(浮点)宽度;
最终浮动SY = dstHeight /(浮点)高度;
m.setScale(SX,SY);
位图B = Bitmap.createBitmap(源,0,0,宽度,高度,M滤波器); 同步(Bitmap.class){
//我们需要检查空?为什么不只是每次分配?
如果(sScaleMatrix == NULL){
sScaleMatrix =米;
}
} 返回b;
}
src是位图
I have following code:
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mRealPaint);
}
When i run it, it runs fine for few pages. Then after few pictures are done it force closes giving following exception:
E/GraphicsJNI(493): VM won't let us allocate 921600 bytes
D/AndroidRuntime(493): Shutting down VM
W/dalvikvm(493): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(493): FATAL EXCEPTION: main
E/AndroidRuntime(493): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I read many posts on this topic. Updated Large heap to true, but it is still not working. Can someone please help me tell what am i doing wrong here?
More errors:
W/webcore(541): Can't get the viewWidth after the first layout
D/webviewglue(541): nativeDestroy view: 0x40a7b8
I/Ads(541): onReceiveAd()
D/dalvikvm(541): GC_EXTERNAL_ALLOC freed 1943 objects / 219448 bytes in 227ms
D/dalvikvm(541): GC_EXTERNAL_ALLOC freed 707 objects / 100576 bytes in 159ms
E/ActivityThread(541): Failed to find provider info for com.google.plus.platform
解决方案
try the following code :-
public static Bitmap createScaledBitmap(Bitmap src, int dstWidth,
int dstHeight, boolean filter) {
Matrix m;
synchronized (Bitmap.class) {
// small pool of just 1 matrix
m = sScaleMatrix;
sScaleMatrix = null;
}
if (m == null) {
m = new Matrix();
}
final int width = src.getWidth();
final int height = src.getHeight();
final float sx = dstWidth / (float)width;
final float sy = dstHeight / (float)height;
m.setScale(sx, sy);
Bitmap b = Bitmap.createBitmap(src, 0, 0, width, height, m, filter);
synchronized (Bitmap.class) {
// do we need to check for null? why not just assign everytime?
if (sScaleMatrix == null) {
sScaleMatrix = m;
}
}
return b;
}
src will be your bitmap
这篇关于Android的例外 - 外部分配太大,这一过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文