2012-12-13 22 views
4

我正在使用通用圖像加載程序在ListView中顯示圖像作爲縮略圖但是我收到了內存不足的錯誤,當我滾動列表比新視圖是當列表不在滾動舞臺上時具有第一視圖的圖像,但是圖像被設置爲它應該是的適當圖像。使用通用圖像加載器和刷新圖像的內存不足錯誤

public class NewAdapter extends BaseAdapter { 

    private Activity activity; 
    private ArrayList<String> movieThumbnail; 
    private ArrayList<String> movieText; 
    private static LayoutInflater inflater=null; 
    static File cacheDir; 

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(activity) 
    .memoryCache(new WeakMemoryCache()) 
    .denyCacheImageMultipleSizesInMemory() 
    .discCache(new UnlimitedDiscCache(cacheDir)) 
    .threadPoolSize(5) 
    .imageDownloader(new URLConnectionImageDownloader(120 * 1000, 120 * 1000)) 
    .enableLogging() 
    .build(); 

    DisplayImageOptions options = new DisplayImageOptions.Builder() 
    .cacheOnDisc() 
    .cacheInMemory() 
    .bitmapConfig(Bitmap.Config.RGB_565) 
    .imageScaleType(ImageScaleType.IN_SAMPLE_INT) 
    .build(); 

    private ImageLoader imageLoader= ImageLoader.getInstance(); 

    public NewAdapter(Activity a, ArrayList<String> movieThumbnail, ArrayList<String> movieText) { 
     activity = a; 
     /*data=d;*/ 
     this.movieThumbnail = movieThumbnail; 
     this.movieText = movieText; 
     inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) 
      cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"JunkFolder"); 
     else 
      cacheDir=activity.getCacheDir(); 
     if(!cacheDir.exists()) 
      cacheDir.mkdirs(); 

     imageLoader.init(config); 

    } 

    public int getCount() { 
     return movieText.size(); 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi=convertView; 
     if(convertView==null) 
       vi = inflater.inflate(R.layout.listrow, null); 

     TextView text=(TextView)vi.findViewById(R.id.rowListTextView); 
     ImageView image=(ImageView)vi.findViewById(R.id.movieImage); 
     text.setText(movieText.get(position)); 
     imageLoader.displayImage(movieThumbnail.get(position), image, options); 
     return vi; 
    } 
} 

這裏是一個XML佈局的ImageView和TextView的

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/text_relativelayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/latest_list_toggle" /> 


    <ImageView 
     android:id="@+id/movieImage" 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_marginLeft="12dp" 
     android:layout_marginTop="14dp" /> 

    <TextView 
     android:id="@+id/rowListTextView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/movieImage" 
     android:paddingBottom="10dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="21dp" 
     android:paddingTop="20dp" 
     android:text="@string/app_name" 
     android:textColor="@android:color/black" 
     android:textSize="17sp" 
     android:textStyle="bold" /> 
</RelativeLayout> 
+0

我解決了在listView刷新上設置適當圖像的問題。之前我沒有在調用Imageloader.displayImage()時傳遞DisplayImageOption的變量。現在它的工作正常。 :) –

回答

11

設置.resetViewBeforeLoading()在DisplayImageOptions。下一個(所有的人或幾個)

+0

我正在加載一個圖像與基地適配器,其中我設置在圖像與inflater imageview與畢加索,也試圖與圖像加載器,但同時加載圖像的地方持有人我要越來越內存異常與通用圖像加載器和特別是三星s4設備的畢加索 我也使用最新的畢加索罐,但沒有得到解決方案 https://github.com/square/picasso/issues/623 我已經嘗試大髖=真和bitmap.recycle但無法解決它 –

1

確保你的圖像大小需要你ImageLoader的

// 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); 

      // Find the correct scale value. It should be the power of 2. 
      final int REQUIRED_SIZE = 70; 
      int width_tmp = o.outWidth, height_tmp = o.outHeight; 
      int scale = 1; 
      while (true) { 
       if (width_tmp/2 < REQUIRED_SIZE 
         || height_tmp/2 < REQUIRED_SIZE) 
        break; 
       width_tmp /= 2; 
       height_tmp /= 2; 
       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; 
    } 
4

嘗試:

減少線程池大小配置(.threadPoolSize(...))。建議使用1 - 5。 使用

.bitmapConfig(Bitmap.Config.RGB_565)  

顯示選項。 RGB_565中的位圖比ARGB_8888消耗的內存少2倍。 使用

.memoryCache(new WeakMemoryCache()) 

在內存配置或禁用緩存在顯示選項所有 (不叫.cacheInMemory())。 使用

.imageScaleType(ImageScaleType.IN_SAMPLE_INT) 

顯示選項。或者試試

.imageScaleType(ImageScaleType.EXACTLY). 

避免使用RoundedBitmapDisplayer。它會創建一個帶有ARGB_8888配置的新Bitmap對象,用於在工作中顯示。

相關問題