[英]Out of memory when select image from gallery for the third times Android
I have a app need to load large images (2mb) for serveral times from the gallery and show in an imageview. 我有一个应用程序需要从图库中加载大量图像(2mb),以达到服务器所需的时间,并在imageview中显示。 I want to allow user to change the image after they select.
我想允许用户在选择后更改图像。 So they can get back to previous activity and select again.
这样他们就可以返回上一个活动并再次选择。 This works fine for the first two times, however, on the third time, it crush for out of memory.
这在前两次都可以正常工作,但是在第三次,它会因为内存不足而崩溃。
[dalvikvm-heap] Out of memory on a 32741392-byte allocation. [dalvikvm-heap] 32741392字节分配的内存不足。
[BitmapFactory] decoder JPEG decode fail! [BitmapFactory]解码器JPEG解码失败!
[skia] --- decoder->decode returned false [skia] ---解码器->解码返回false
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
uri = data.Data;
_paintView.SetImageURI (uri);
string path = GetPathToImage (uri);
Toast.MakeText (this, path, ToastLength.Long);
}
if (uri != null) {
//at the third time crush at this line
_paintView._Bmp= Android.Provider.MediaStore.Images.Media.GetBitmap(this.ContentResolver, uri);
_paintView._Bmp= _paintView._Bmp.Copy(Android.Graphics.Bitmap.Config.Argb8888, true);
_paintView._Bmp=Bitmap.CreateScaledBitmap(_paintView._Bmp, _paintView.w, _paintView.h, false);
_paintView._Canvas = new Canvas(_paintView._Bmp);
_paintView.SetImageBitmap (_paintView._Bmp);
uri.Dispose ();
_paintView2.SetImageBitmap (null);
_bitmap = _paintView._Bmp;
}
}
So I add a bitmap recycle at the OnBackPressed() and want to free the memory this activity consuming 因此,我在OnBackPressed()处添加了一个位图回收,并希望释放此活动消耗的内存
public override void OnBackPressed ()
{
base.OnBackPressed();
//_paintView._Bmp and _paintView2._Bmp contain the bitmap I use
if (_paintView._Bmp != null) {
_paintView._Bmp.Recycle ();
_paintView._Bmp = null;
}
if (_paintView2._Bmp != null) {
_paintView2._Bmp.Recycle ();
_paintView2._Bmp = null;
}
base.OnDestroy ();
StartActivity(new Intent(this, typeof(MainActivity)));
Finish();
}
However, it still doesn't work. 但是,它仍然不起作用。 I just basically don't want to resize the bitmap, since I will do some image processing stuff and low quality will make it looks bad.
我只是基本上不想调整位图的大小,因为我会做一些图像处理工作,而低质量会使它看起来很糟糕。 So how can I free the memory after I use?
那么,使用后如何释放内存?
The first time successfully load 第一次成功加载
[skia] jpeg_decoder mode 1, config 6, w 3504, h 2336, sample 1, bsLength 0!! [skia] jpeg_decoder模式1,配置6,w 3504,h 2336,样本1,bsLength 0!
[dalvikvm-heap] Grow heap (frag case) to 37.557MB for 32741392-byte allocation [dalvikvm-heap]将堆(片段大小写)增加到37.557MB,以分配32741392字节的分配
[skia] jpeg_decoder finish successfully, L:1881!!! [skia] jpeg_decoder成功完成,L:1881!
[ManageCursor] managedQuery, c=1108461336 [ManageCursor] managedQuery,c = 1108461336
[ManageCursor] startManagingCursor, c=1108461336 [ManageCursor] startManagingCursor,c = 1108461336
[skia] jpeg_decoder mode 1, config 6, w 3504, h 2336, sample 1, bsLength 0!! [skia] jpeg_decoder模式1,配置6,w 3504,h 2336,样本1,bsLength 0!
[dalvikvm-heap] Grow heap (frag case) to 68.783MB for 32741392-byte allocation [dalvikvm-heap]将堆(frag大小写)增加到68.783MB,以分配32741392字节的分配
[skia] jpeg_decoder finish successfully, L:1881!!! [skia] jpeg_decoder成功完成,L:1881!
Is this mean I use too much memory for select one image? 这是否意味着我使用太多内存来选择一张图像? Should I resize bitmap or free memory?
我应该调整位图的大小还是可用内存? Thank yous very much!
非常感谢您!
You're image is to big. 您的形象很大。 You should load it in a smaller size because a
Bitmap
takes up 4 bytes for a pixels. 您应该以较小的大小加载它,因为
Bitmap
占用4个字节的像素。 So a 5MP image, will result in 20MB of memory. 因此,一个5MP的映像将导致20MB的内存。
You need to load in the Bitmap's meta data, recalculate the required sample size, and request the Bitmap in that size. 您需要加载位图的元数据,重新计算所需的样本大小,然后以该大小请求位图。 This can be done with
Bitmap.Options
. 这可以通过
Bitmap.Options
完成。 Read all about it in the Developer guide. 在开发人员指南中阅读所有相关内容。 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.