如何处理内存不足的错误? [英] How to handle out of memory error?

查看:104
本文介绍了如何处理内存不足的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code显示位图在我的ImageView。 当我尝试加载的大小,例如大于1.5MB的形象它给我的错误。 任何一个建议我的解决方案?

 尝试{

                         URL aURL =新的URL(myRemoteImages [VAL]);
                         URLConnection的康恩= aURL.openConnection();

                         conn.connect();
                         InputStream的是= NULL;
                         尝试
                         {
                             是= conn.getInputStream();
                         }赶上(IOException异常E)
                         {


                             返回0;

                         }
                         诠释一个= conn.getConnectTimeout();
                         的BufferedInputStream双=新的BufferedInputStream(是);

                         位图BM;
                         尝试
                         {
                             BM = BitmapFactory.de codeStream(之二);
                         }赶上(例外前)
                         {
                             bis.close();
                             is.close();
                             返回0;
                         }
                         bis.close();
                         is.close();
                         img.setImageBitmap(BM);

                    }赶上(IOException异常E){
                        返回0;
                    }

                    返回1;
 

登录猫:

 十二月六号至14日:03:11.701:ERROR / AndroidRuntime(443):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
12月6日至14号:03:11.861:ERROR / AndroidRuntime(443):java.lang.OutOfMemoryError:位图大小超过VM预算
12月6日至14号:03:11.861:ERROR / AndroidRuntime(443):在android.graphics.BitmapFactory.nativeDe codeStream(本机方法)
 

解决方案

您应该取消code与inSampleSize选项,以减少内存消耗。 <一href="http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966">http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966

另一种选择inJustDe codeBounds可以帮助你找到正确的inSampleSize值的http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

I am using following code to display bitmap in my ImageView. When I try to load image of size for example bigger than 1.5MB it give me error. Any one suggest me solution?

  try {  

                         URL aURL = new URL(myRemoteImages[val]);  
                         URLConnection conn = aURL.openConnection(); 

                         conn.connect();  
                         InputStream is = null;
                         try
                         {
                             is= conn.getInputStream();  
                         }catch(IOException e)
                         {


                             return 0;

                         }
                         int a=  conn.getConnectTimeout();
                         BufferedInputStream bis = new BufferedInputStream(is);  

                         Bitmap bm;
                         try
                         {
                             bm = BitmapFactory.decodeStream(bis);
                         }catch(Exception ex)
                         {
                             bis.close(); 
                             is.close();  
                             return 0; 
                         }
                         bis.close();  
                         is.close();  
                         img.setImageBitmap(bm);

                    } catch (IOException e) {  
                        return 0;
                    }  

                    return 1;

Log cat:

06-14 12:03:11.701: ERROR/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception
06-14 12:03:11.861: ERROR/AndroidRuntime(443): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-14 12:03:11.861: ERROR/AndroidRuntime(443):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)

解决方案

You should decode with inSampleSize option to reduce memory consumption. http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue/823966#823966

Another option inJustDecodeBounds can help you to find correct inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

这篇关于如何处理内存不足的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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