2013-08-22 73 views
1

我通过许多资源推荐,但无法得到正确的答案,滚动Listview时内存不足异常?

我已经做了一个自定义适配器来查看图像在列表视图。这些图像从存储卡中检索。一切运行良好,但是当我滚动列表视图时,我得到一个OutOfMemory异常。我发布了代码,使用我从SD卡中检索图像。

public void getFromSdcard() { 

    File file = new File(
      android.os.Environment.getExternalStorageDirectory(), 
      "Tiles/.NoMedia"); 

    if (file.isDirectory()) { 
     listFile = file.listFiles(); 

     for (int i = 0; i < listFile.length; i++) { 

      f.add(listFile[i].getAbsolutePath()); 

     } 
    } 
} 

这里f是字符串数组列表,我将它传递给下一个自定义适配器是我的自定义接口的代码。

public class NewImageAdapter extends ArrayAdapter<Image> { 

private ArrayList<Image> objects; 
String packageName; 
Activity act; 

public NewImageAdapter(Activity context, int image_layout, 
     ArrayList<Image> objects) { 
    super(context, image_layout, objects); 
    this.act = context; 
    this.objects = objects; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 

    View v = convertView; 

    if (v == null) { 
     LayoutInflater inflater = (LayoutInflater) getContext() 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     v = inflater.inflate(R.layout.list_item, null); 
    } 

    Image i = objects.get(position); 

    if (i != null) { 
     ImageView iv = (ImageView) v.findViewById(R.id.imagemenu123); 
     // TextView tv = (TextView) v.findViewById(R.id.commandText); 
     if (iv != null) { 

      Bitmap myBitmap = BitmapFactory.decodeFile(i.getImagePath()); 

      iv.setImageBitmap(myBitmap); 
      // iv.setImageBitmap(i.getImageBitmap()); 
      // tv.setText("Tiles Images"); 
     } 

    } 
    return v; 
} 

}

任何解决我的问题:

回答

5

使用这个概念,这将帮助你,在那之后设置图像视图

public static Bitmap convertBitmap(String path) { 

     Bitmap bitmap=null; 
     BitmapFactory.Options bfOptions=new BitmapFactory.Options(); 
     bfOptions.inDither=false;      //Disable Dithering mode 
     bfOptions.inPurgeable=true;     //Tell to gc that whether it needs free memory, the Bitmap can be cleared 
     bfOptions.inInputShareable=true;    //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future 
     bfOptions.inTempStorage=new byte[32 * 1024]; 


     File file=new File(path); 
     FileInputStream fs=null; 
     try { 
      fs = new FileInputStream(file); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     try { 
      if(fs!=null) 
      { 
       bitmap=BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions); 
      } 
      } catch (IOException e) { 

      e.printStackTrace(); 
     } finally{ 
      if(fs!=null) { 
       try { 
        fs.close(); 
       } catch (IOException e) { 

        e.printStackTrace(); 
       } 
      } 
     } 

     return bitmap; 
    } 

的imagebitmap如果你想从高度和宽度像60和60大的图像小图像和滚动列表视图快然后使用这个概念

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
      int reqHeight) { 

     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap bmp = BitmapFactory.decodeFile(path, options); 
     return bmp; 
     } 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 

     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
      } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
      } 
     } 
     return inSampleSize; 
     } 

我希望它会帮助你很多。

您凸轮采取从开发者网站Here

+0

希望它的工作.... Thanx ... @ Sunil ... –

+0

U欢迎投票... –

0

出的存储装置,以及你需要加载图像在ListView只有当他们在视图,并考虑释放他们时,没有 - 动态加载它们。

您也可以尝试在该适配器中合并ViewHolder类。

0

可能是你可以试试这个:

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length) 
iv.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false)); 

希望它帮助!

+0

谢谢你,但我已经尝试过这种解决方案,这不适合我... –

+0

我认为你应该发布logcat的问题,以更好的解决问题。 –

-1

帮助其实我觉得最好的办法是找到错误振振有辞。对于这个“Out of Memroy”的主要共同点是,当你滚动gcc分配内存时,即使你从下往上滚动,它也会为listview分配单独的内存,换句话说,你的memeory会被浪费或多余的使用。即使在位图中,您可以通过缩放选项减少emeroy分配大小,但它是永久性解决方案的短期解决方案,我发现通过查找d卡中的图像数量以及滚动比较poistion时为每个listview创建视图数组当poistion的大小等于view-1的大小时,你已经分配了end net unique memeroy,如果你再次向上或向下滚动,则不会在listview中分配memroy,因为你已经到达视图数组的末尾,没有furthur需要分配memeroy,因此问题解决

+0

我不说wo,但我想明确说明我有解释你背后真正的问题内存不足错误,并且使用位图提供长期解决方案并不能解决问题 – Joker