随着线程加载图像导致OOM增加堆

时间:2014-06-15 12:53:49

标签: android

我在这里面对一个真正的问题,这里我有一个带有过滤部分和图像的列表视图,没有图像我的堆在5mb上稳定,但只有3个图像,它提高到27 mb!所以我决定使用威胁这样做:

  @Override
public View getView(final int position, View convertView, ViewGroup parent) {




     if (convertView == null) {
        holder = new ViewHolder();
        int type = getItemViewType(position);

        switch (type) {
        case TYPE_ITEM:
            convertView = mLayoutInflater.inflate(R.layout.row_view, 
 null);
             holder.imageview  = 
 (ImageView)convertView.findViewById(R.id.imageView3);

            Thread t= new Thread(new Runnable() {
                  @Override
                  public void run() {

             String name=mListItems.get(position).toString();
             String namer[] = name.split("\\@");
             String imagenamer=namer[0];

        //   String imagenamer= mListItems.get(position).toString();
             Uri uri1=Uri.parse("storage/sdcard/LifeMatePrivate
 /ProfileImage/ProfileImage,imagechange_1,"+imagenamer+".jpg");
             Uri uri2=Uri.parse("storage/sdcard/LifeMatePrivate
/ProfileImage/Default.jpg");
             File file = new File("storage/sdcard/LifeMatePrivate
/ProfileImage/ProfileImage,imagechange_1,"+imagenamer+".jpg");
         if (file.exists()){
             System.out.println("exists");
             holder.imageview.setImageURI(uri1);
        }else{
            holder.imageview.setImageURI(uri2);
            System.out.println("storage/sdcard/LifeMatePrivate
 /ProfileImage/ProfileImage,imagechange_1,"+imagenamer+".jpg"+"didnt exist"); 
        }}
        });
             t.start();     

             break;
        case TYPE_SECTION:
            convertView = 
 mLayoutInflater.inflate(R.layout.section_row_view, null);
            break;
        }
        holder.textView = (TextView)   
 convertView.findViewById(R.id.row_title);
        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }



    holder.textView.setText(mListItems.get(position).toString());

    return convertView;
}

但现在当我运行应用程序时,我的内存异常直接出现,意味着它变得更糟!这对男人来说是什么意思?我怎样才能解决我的记忆问题呢?

使用毕加索:(我的方法好吗?)

 Context mContext;

public PinnedHeaderAdapter(Context context, ArrayList<String> 
 listItems,ArrayList<Integer> listSectionPos) {
    this.mContext = context;
    this.mListItems = listItems;
    this.mListSectionPos = listSectionPos;

    mLayoutInflater = (LayoutInflater) 
 mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return mListItems.size();
}

@Override
public boolean areAllItemsEnabled() {
    return false;
}

@Override
public boolean isEnabled(int position) {
    return !mListSectionPos.contains(position);
}

@Override
public int getViewTypeCount() {
    return TYPE_MAX_COUNT;
}

@Override
public int getItemViewType(int position) {
    return mListSectionPos.contains(position) ? TYPE_SECTION : TYPE_ITEM;
}

@Override
public Object getItem(int position) {
    return mListItems.get(position);
}

@Override
public long getItemId(int position) {
    return mListItems.get(position).hashCode();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder = null;



     if (convertView == null) {
        holder = new ViewHolder();
        int type = getItemViewType(position);

        switch (type) {
        case TYPE_ITEM:
            convertView = mLayoutInflater.inflate(R.layout.row_view, 
  null);
             holder.imageview  = 
 (ImageView)convertView.findViewById(R.id.imageView3);

              String name=mListItems.get(position).toString();
             String namer[] = name.split("\\@");
             String imagenamer=namer[0];

        //   String imagenamer= mListItems.get(position).toString();


             File file = new File("storage/sdcard/LifeMatePrivate
 /ProfileImage/ProfileImage,imagechange_1,"+imagenamer+".jpg");
         if (file.exists()){

             Uri uri1=Uri.parse("storage/sdcard/LifeMatePrivate
 /ProfileImage/ProfileImage,imagechange_1,"+imagenamer+".jpg");
             System.out.println("exists");
           //  holder.imageview.setImageURI(uri1);

             Picasso.with(mContext)
              .load(uri1)
              .resize(50, 50)
              .centerCrop()
              .into(holder.imageview);



         }else{

            Uri uri2=Uri.parse("storage/sdcard/LifeMatePrivate
 /ProfileImage/Default.jpg");
        //  holder.imageview.setImageURI(uri2);
            System.out.println("storage/sdcard/LifeMatePrivate
 /ProfileImage/ProfileImage,imagechange_1,"+imagenamer+".jpg"+"didnt exist"); 

            Picasso.with(mContext)
              .load(uri2)
              .resize(70, 70)
              .centerCrop()
              .into(holder.imageview);
         }

             break;
        case TYPE_SECTION:
            convertView = 
 mLayoutInflater.inflate(R.layout.section_row_view, null);
            break;
        }
        holder.textView = (TextView)  
 convertView.findViewById(R.id.row_title);
        convertView.setTag(holder);


    } else {
        holder = (ViewHolder) convertView.getTag();
    }



    holder.textView.setText(mListItems.get(position).toString());

    return convertView;
}

1 个答案:

答案 0 :(得分:0)

square.github.io/picasso使用此lib在适配器中显示图像。这个lib将解决您的问题。