java.lang.OutOfMemoryError:无法分配与15257484免费字节一个字节31961100分配和14MB直到OOM [英] java.lang.OutOfMemoryError: Failed to allocate a 31961100 byte allocation with 15257484 free bytes and 14MB until OOM

查看:346
本文介绍了java.lang.OutOfMemoryError:无法分配与15257484免费字节一个字节31961100分配和14MB直到OOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做迈上了一个位图如下图所示。

I am doing scaled a bit map like below

 Bitmap resizedBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(picturePath), 960, 730, false);

这样的处理我是上传图片有时我得到以下类型的错误

doing this processing i am upload a images some times i am getting below type error

java.lang.OutOfMemoryError: Failed to allocate a 31961100 byte allocation with 15257484 free bytes and 14MB until OOM

请帮我如何解决?

推荐答案

这是如何使用 BitmapFactory.Options

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inSampleSize = 2;
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[16 * 1024];

Bitmap bmp = BitmapFactory.decodeFile(picturePath,options);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, 960, 730, false);

您也可以计算出 inSampleSize 为您的位图通过编写自定义的功能。

Also you can calculate the inSampleSize for your bitmap by writing a custom function.

下面是谷歌文档: http://developer.android.com/培训/显示的位图/负载bitmap.html

您可以通过添加安卓增加分配给您的应用程序内存:largeHeap =真正的在清单

注意:增加为您的应用程序不被认为是一个理想的解决方案。

Note : Increasing heap for your application doesn't considered to be a ideal solution.

下面是从谷歌,说明它的提取物,

Here's the extract from google that explains it,

不过,要求一大堆的能力,仅用于一
  小集合的应用程序,可以证明需要消耗更多的内存(如
  作为一个大的照片编辑应用程序)。一定不要简单地要求一大堆
  因为你已经用完了内存,你需要速战速决,你应该
  使用它,只有当你确切地知道,所有的内存被
  分配以及为什么必须保留。然而,即使你有信心
  您的应用程序可以证明一大堆,你应该避免请求
  无论尽可能。利用额外的内存将越来越多地
  到整体用户体验,因为垃圾造成损害
  收集将需要更长的时间和系统性能可能会较慢时,
  任务切换或执行其他常见操作。

However, the ability to request a large heap is intended only for a small set of apps that can justify the need to consume more RAM (such as a large photo editing app). Never request a large heap simply because you've run out of memory and you need a quick fix—you should use it only when you know exactly where all your memory is being allocated and why it must be retained. Yet, even when you're confident your app can justify the large heap, you should avoid requesting it to whatever extent possible. Using the extra memory will increasingly be to the detriment of the overall user experience because garbage collection will take longer and system performance may be slower when task switching or performing other common operations.

这里的文档的完整链接 https://developer.android.com/training /articles/memory.html

here's the complete link of the documentation https://developer.android.com/training/articles/memory.html

这篇关于java.lang.OutOfMemoryError:无法分配与15257484免费字节一个字节31961100分配和14MB直到OOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆