Para tentar melhorar a performance da listagem da agenda, fiz um ViewHolder conforme recomendado, porém estou tomando a exception OutOfMemory quando dou um BitmapFactory.decodeFile.
Segue trecho do código do meu adapter:
AlunosAdapter.java
public View getView(int position, View convertView, ViewGroup parent) {
View view;
ViewHolder holder;
if (convertView == null){
view = LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
holder = new ViewHolder(view);
view.setTag(holder);
} else {
view = convertView;
holder = (ViewHolder) view.getTag();
}
Aluno aluno = (Aluno) getItem(position);
holder.nome.setText(aluno.getNome());
holder.telefone.setText(aluno.getTelefone());
if(aluno.getPathFoto() != null) {
Bitmap bitmap = BitmapFactory.decodeFile(aluno.getPathFoto());
holder.foto.setImageBitmap(bitmap);
}
return view;
}
ViewHolder.java
public class ViewHolder {
public final TextView nome;
public final TextView telefone;
public final ImageView foto;
public ViewHolder(View view){
nome = (TextView) view.findViewById(R.id.item_nome);
telefone = (TextView) view.findViewById(R.id.item_telefone);
foto = (ImageView) view.findViewById(R.id.item_foto);
}
}
Exception:
java.lang.OutOfMemoryError: Failed to allocate a 64256268 byte allocation with 16777216 free bytes and 25MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStreamInternal(BitmapFactory.java:635)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:611)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:391)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:417)
at com.example.luizpacinni.agenda.adapter.AlunosAdapter.getView(AlunosAdapter.java:63)