关于创建位图的java.lang.OutOfMemoryError

时间:2015-08-21 04:25:02

标签: java android bitmap

 private static Bitmap getScaledBitmap(Bitmap bitmap, RelativeLayout
            relativeLayout, Context context) {
        try {
            Bitmap background =
                    Bitmap.createBitmap((int) relativeLayout.getWidth(), (int)
                            relativeLayout.getHeight(), Bitmap.Config.ARGB_8888);
            float
                    originalWidth = bitmap.getWidth(), originalHeight =
                    bitmap.getHeight();
            Canvas canvas = new Canvas(background);
            float
                    scale = relativeLayout.getWidth() / originalWidth;
            float xTranslation
                    = 0.0f, yTranslation = (relativeLayout.getHeight() - originalHeight * scale) / 2.0f;
            Matrix transformation = new Matrix();
            transformation.postTranslate(xTranslation, yTranslation);
            transformation.preScale(scale, scale);
            Paint paint = new Paint();
            paint.setFilterBitmap(true);
            canvas.drawBitmap(bitmap,
                    transformation, paint);
            try {
                RenderScript rs =
                        RenderScript.create(context);
                Allocation inputAlloc =
                        Allocation.createFromBitmap(rs, background);
                ScriptIntrinsicBlur blur
                        = ScriptIntrinsicBlur.create(rs, inputAlloc.getElement());
                blur.setRadius(25);
                blur.setInput(inputAlloc);
                Bitmap blurredBitmap =
                        Bitmap.createBitmap(background.getWidth(), background.getHeight(),
                                background.getConfig());
                Allocation outAlloc =
                        Allocation.createFromBitmap(rs, blurredBitmap);
                blur.forEach(outAlloc);
                outAlloc.copyTo(blurredBitmap);
                rs.destroy();
                return blurredBitmap;
            } catch (Exception e) {
                L.e("getScaledBitmap:
                        RenderScript:" + e); return background; } } catch(Exception e) {
                L.e("getScaledBitmap: " + e);
            } return bitmap;
        } 

//错误引起:

java.lang.OutOfMemoryError at
android.graphics.BitmapFactory.nativeDecodeStream(Native Method) at
android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:613)
at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:589)
at
android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:627)
at
net.databeat.android.imsmediaplayer.player.utils.ImageLoadTask.doInBackground(ImageLoadTask.java:
75)
at
net.databeat.android.imsmediaplayer.player.utils.ImageLoadTask.doInBackground(ImageLoadTask.java:
31)
List item

1 个答案:

答案 0 :(得分:0)

您可以通过为java

指定Xmx参数来增加虚拟机分配的内存
java -Xmx2048m