2012-11-08 108 views
0

我实现了listview用图像和framelayout(含Linearlayout和按钮)应用程序,当我在listview多次滚动从上到下再经过一段时间的应用程序被撞坏给错误:的OutOfMemoryError崩溃

outofMemoryError.

+1

很可能你没有释放图像或加载太多的图像。发布Logcat输出。 – PravinCG

回答

0

作为由Fedor给出的伟大答案,你应该做下面的事情来解决你的问题。

要解决内存不足你应该做这样的事情:

BitmapFactory.Options options=new BitmapFactory.Options(); 
options.inSampleSize = 8; 
Bitmap preview_bitmap=BitmapFactory.decodeStream(is,null,options); 

这inSampleSize选项可减少内存消耗。

这是一个完整的方法。首先它读取图像大小而不解码内容本身。然后它找到最好的inSampleSize值,它应该是2的幂。最后,图像被解码。

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //The new size we want to scale to 
     final int REQUIRED_SIZE=70; 

     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 

你可以参考Here更多描述。希望它能帮助你。