滑行和 java.lang.OutOfMemoryError

Glide and java.lang.OutOfMemoryError

我正在开发一个应用程序,可以显示所有歌曲的专辑封面图片。所以我使用 glide 加载和缓存图像并避免 OutofMemoryError 但我仍然收到该错误:

11-11 11:05:55.866 11120-11120/com.xrobot.andrew.musicalbumsE/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.xrobot.andrew.musicalbums, PID: 11120 java.lang.OutOfMemoryError: OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available 

这是我在 AlbumAdapter 中的 getView:

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

    RelativeLayout albumsLay = (RelativeLayout)songInf.inflate
            (R.layout.album_layout, parent, false);
    ImageView coverView = (ImageView)albumsLay.findViewById(R.id.song_cover);

    //get song using position
    Song currSong = songs.get(position);

    if (Drawable.createFromPath(currSong.getCover()) != null) {
        Drawable img = Drawable.createFromPath(currSong.getCover());
        Glide.with(mContext).load("").placeholder(img).override(50,50).into(coverView);
    }

    albumsLay.setTag(position);
    return albumsLay;
}

尝试直接使用带有图片路径的 Glide:

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

    RelativeLayout albumsLay = (RelativeLayout)songInf.inflate
            (R.layout.album_layout, parent, false);
    ImageView coverView = (ImageView)albumsLay.findViewById(R.id.song_cover);

    //get song using position
    Song currSong = songs.get(position);

    // If you are sure currSong.getCover() exists you can remove the if statement
    if(new File(currSong.getCover().exists))
        Glide.with(mContext).load(currSong.getCover()).override(50,50).into(coverView);


    albumsLay.setTag(position);
    return albumsLay;
}

您还可以使用支架来查看视图。这将减少内存使用量:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
     CoverHolder holder = null;
     if (convertView == null) {
         convertView = mInflater.inflate(R.layout.album_layout, null);
         holder = new CoverHolder();
         holder.coverView = (ImageView)convertView.findViewById(R.id.song_cover);
         convertView.setTag(holder);
     } else {
         holder = (CoverHolder)convertView.getTag();
     }
    Glide.with(mContext).load(currSong.getCover()).override(50,50).into(holder.coverView);
    return convertView;
}

// The holder
public static class CoverHolder{
    public ImageView coverView;
}

不过,如果你真的需要在一个巨大的清单上表现。你可以看看 RecyclerView here.