【问题标题】:java.lang.OutOfMemoryError on creating bitmapjava.lang.OutOfMemoryError 创建位图
【发布时间】:2015-11-14 22:16:07
【问题描述】:
 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

【问题讨论】:

标签: java android bitmap


【解决方案1】:

你可以通过给java指定Xmx参数来增加虚拟机分配的内存

java -Xmx2048m

【讨论】:

  • 在命令行中作为 java 参数。例如,如果您使用 Eclipse,则放置此参数的位置是右键单击 java 文件>>属性>>单击启动配置下的行>>单击编辑按钮>>从弹出窗口中选择参数选项卡>>在 VM 参数框中输入值 -Xmx2048m >> 单击确定
猜你喜欢
  • 2014-07-06
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-16
  • 1970-01-01
  • 2011-04-26
相关资源
最近更新 更多