- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我的应用在源代码中的以下行遇到 OOM 错误:
image = BitmapFactory.decodeStream(assetManager.open(imgFilename));
就在导致应用因 OOM 错误而终止的分配之前:
(...)
08-05 21:22:12.443: I/dalvikvm-heap(2319): Clamp target GC heap from 25.056MB to 24.000MB
08-05 21:22:12.443: D/dalvikvm(2319): GC_FOR_MALLOC freed <1K, 50% free 2709K/5379K, external 18296K/19336K, paused 58ms
08-05 21:22:14.513: D/dalvikvm(2319): GC_EXTERNAL_ALLOC freed <1K, 50% free 2709K/5379K, external 18296K/19336K, paused 101ms
08-05 21:22:14.903: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:22:14.903: D/dalvikvm(2319): GC_FOR_MALLOC freed 0K, 50% free 2709K/5379K, external 18312K/19336K, paused 53ms
08-05 21:22:22.843: D/ddm-heap(2319): Heap GC request
08-05 21:22:22.963: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:22:22.963: D/dalvikvm(2319): threadid=1: still suspended after undo (sc=1 dc=1)
08-05 21:22:22.963: D/dalvikvm(2319): GC_EXPLICIT freed 1K, 50% free 2710K/5379K, external 18312K/19336K, paused 116ms
DDMS 报告了关于堆状态的类似图片:
Heap Size: 5.254 MB
Allocated: 2.647 MB
Free: 2.607 MB
%Used: 50.38%
#Objects 49,028
单步越过此行会导致 OOM 错误:
08-05 21:26:04.783: D/dalvikvm(2319): GC_EXTERNAL_ALLOC freed <1K, 50% free 2710K/5379K, external 18312K/19336K, paused 57ms
08-05 21:26:05.023: E/dalvikvm-heap(2319): 2097152-byte external allocation too large for this process.
08-05 21:26:05.163: I/dalvikvm-heap(2319): Clamp target GC heap from 25.073MB to 24.000MB
08-05 21:26:05.163: E/GraphicsJNI(2319): VM won't let us allocate 2097152 bytes
08-05 21:26:05.163: D/dalvikvm(2319): GC_FOR_MALLOC freed 0K, 50% free 2710K/5379K, external 18312K/19336K, paused 30ms
08-05 21:26:05.283: D/skia(2319): --- decoder->decode returned false
此应用面向 Android 2.2 及更高版本。
提前致谢!
最佳答案
Android 库在加载图像方面并不那么聪明,因此您必须为此创建解决方法。
在我的测试中,Drawable.createFromStream
比 BitmapFactory.decodeStream
使用更多的内存。
您可以更改配色方案以减少内存 (RGB_565),但图像也会降低质量:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Config.RGB_565;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
引用:http://developer.android.com/reference/android/graphics/Bitmap.Config.html
您还可以加载缩放的图像,这将大大减少内存使用量,但您必须知道您的图像不会损失太多质量。
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
引用:http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
要动态定义 inSampleSize,您可能想知道图像大小以做出决定:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeStream(stream, null, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
options.inJustDecodeBounds = false;
// recreate the stream
// make some calculation to define inSampleSize
options.inSampleSize = ?;
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
您可以根据设备的屏幕尺寸自定义 inSampleSize。要获取屏幕大小,您可以执行以下操作:
DisplayMetrics metrics = new DisplayMetrics();
((Activity) activity).getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenWidth = metrics.widthPixels;
int screenHeight = metrics.heightPixels;
其他教程:
关于Android:BitmapFactory.decodeStream() 内存不足,400KB 文件和 2MB 可用堆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11820266/
通过使用方法 BitmapFactory.decodeStream(Resources res,int id) 和 BitmapFactory.decodeFile(String pathName)
我的应用程序从 SD 卡解码大量位图,因此我想重用现有位图以减少 GC 工作。我看到来自 Android Training 的示例和 this video它非常适合 BitmapFactory.dec
哪种设置 ImageView 图像的方法更好(使用更少的堆空间)? imageView.setImageDrawable(Drawable.createFromPath(path)); 或
我在向 gridview 添加可变背景时遇到 OutOfMemoryError...在 final Bitmap shelfBackground = BitmapFactory.decodeResou
url="http://www.nasa.gov/sites/default/files/styles/946xvariable_height/public/ladee_spin_2_in_motio
我打算编写一个小画廊应用程序。所以我有一个带有图像的 gridview,显示的图像被存储在本地设备上。我得到的是一个类 ImageLoader,它在后台线程 (AsyncTask) 中加载特定路径中的
在我的应用程序中,我有一个文件: private File TEMP_PHOTO_FILE = new File(Environment.getExternalStorageDirectory(),
您好,我正在从名为 image.png 的 png 图像创建位图。图像的尺寸为 75(宽)x 92(高)。当我运行这段代码时: Bitmap bitmap = BitmapFactory.decode
在我的 android 应用程序中,我上传和下载 JPEG 图像。对于某些图像,BitmapFactory 对下载的图像进行下采样,我不明白为什么以及如何阻止它这样做。我在上传和下载图像时记录位图对象
我有同一张图片的两份副本。其中一个在应用程序的资源文件夹(默认可绘制对象)中,另一个在外部存储中。 我用以下代码得到了位图: // Get from storage BitmapFactory.d
嘿,我不确定为什么每次我在图库中选择图片时都会出现这个问题? 代码如下: if (v == uploadImageButton) { // below
今天我的应用程序崩溃了,出现内存不足错误。 java.lang.OutOfMemoryError in android.graphics.BitmapFactory.nativeDecodeAsset
我正在尝试从Camera.PictureCallback onPictureTaken获取Jpeg,但是当我这样做时 bitmapImage = BitmapFactory.decodeByteArr
在研究 Android 的 BitmapFactory.Options 类时,我注意到它的字段是公开可以访问和修改的。 这与一般封装规则相反,该规则规定字段应声明为 private,并且应通过 pub
这段代码给出了运行时异常,我无法捕获它。 BitmapFactory.decodeByteArray 不会返回 null 或类似的内容。 Bitmap bitmap = BitmapFactory.d
我有一个包含两列的 ArrayList,并显示下面带有文本的图像。我正在使用高质量图像,并且需要在 GridView 中以良好的质量显示这些图像,为此我使用 BitmapFactory.Options
我应该从互联网获取图像并将其显示在我的 Activity 中。为此,我使用 asynctask 类。我忘记在我的 list 文件中提及互联网权限,并且应用程序成功运行,没有给出任何异常,并且能够获取模
如何从字节数组构造原始大小的位图? byte[] imageAsBytes = Base64.decode(b64.getBytes(), Base64.DEFAULT); Bitma
我在这里从 Cursor 获取值: if (mProfileCursor.moveToFirst()) { byte[] blob = mProfileCursor.getBl
我正在尝试使用这个: BitmapFactory.decodeFile("logo.jpg") 与我的 apk 相比,我似乎没有将文件:logo.jpg 放在正确的位置。它应该去哪里? 附言我得到的错
我是一名优秀的程序员,十分优秀!