У меня есть список из 70 текстовых элементов со значками изображений (которые хранятся в папке drawables).
Если я запускаю приложение в первый раз и медленно прокручиваю список - исключение не происходит.
Когда приложение запускается в первый раз, и я прокручиваю список с помощью действия «fling», возникает следующее исключение:
java.lang.OutOfMemoryError: bitmap size exceeds VM budget
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:439)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:322)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:688)
at android.content.res.Resources.loadDrawable(Resources.java:1710)
at android.content.res.Resources.getDrawable(Resources.java:585)
После этого, если я убью приложение с помощью DDMS, запустите его снова и прокрутите его с помощью действия fling, исключение не происходит. Таким образом, исключение возникает только в том случае, если приложение установлено и запущено в первый раз.
Вот адаптер, который я использую для списка:
public class AuthorAdapter extends ArrayAdapter<Author> {
private ArrayList<Author> items;
private Context context;
private LayoutInflater inflater;
public AuthorAdapter(Context context, int textViewResourceId, ArrayList<Author> items) {
super(context, textViewResourceId, items);
this.items = items;
this.context = context;
inflater = LayoutInflater.from(this.context);
}
static class ViewHolder {
ImageView authorFace;
TextView authorName;
TextView authorWho;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.autor_data, null);
holder = new ViewHolder();
holder.authorFace = (ImageView) convertView.findViewById(R.id.authorFaceImageView);
holder.authorName = (TextView) convertView.findViewById(R.id.authorNameTextView);
holder.authorWho = (TextView) convertView.findViewById(R.id.authorWhoTextView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Author author = items.get(position);
if (author != null) {
if (holder.authorFace != null) {
String faceFileName = author.getFace();
String facePlaceName = context.getPackageName() + ":drawable/" + faceFileName.subSequence(0, faceFileName.lastIndexOf("."));
int faceFileId = context.getResources().getIdentifier(facePlaceName, null, null);
holder.authorFace.setImageResource(faceFileId);
}
if (holder.authorName != null) {
holder.authorName.setText(author.getName());
}
if (holder.authorWho != null) {
holder.authorWho.setText(author.getWho());
}
}
return convertView;
}
}
Есть ли способ избежать исключения?
Должен ли я поймать исключение и как-то сбежать из памяти?
Любой совет, как это сделать?
Спасибо.