2016-11-11 4 views
1

Я разрабатываю приложение, которое отображает все обложки альбомов песен. Поэтому я использую скольжение для загрузки и кэширования изображений и избежать OutOfMemoryError, но я все еще получаю эту ошибку:Glide и java.lang.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 

Это мой GetView в AlbumAdapter:

@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; 
} 
+0

Разве вы не загружаете (imgPath) ', а не создаете drawable и используете его как' .placeholder() '? –

+0

@ThomasRoulin, что мне делать? – xRobot

ответ

2

Попробуйте использовать 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; 
} 

Тем не менее, если вам действительно нужна производительность в огромном списке. Вы можете посмотреть на RecyclerViewhere.

+0

Спасибо, он работает как шарм :) – xRobot

+1

Отлично :) Я также добавил еще несколько советов в свой ответ. –

 Смежные вопросы

  • Нет связанных вопросов^_^