gpt4 book ai didi

java - Glide 和 java.lang.OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-29 19:29:06 24 4
gpt4 key购买 nike

我正在开发一个应用程序来显示歌曲的所有专辑封面图像。所以我使用 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;
}

您还可以为 View 使用支架。这将减少内存使用量:

@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 .

关于java - Glide 和 java.lang.OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40545827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com