【发布时间】:2017-09-11 13:43:15
【问题描述】:
我需要将可绘制文件夹上的一堆图像加载到 imageview 列表中。我收到 java.lang.OutOfMemoryError: Failed to allocate a 17280012 byte allocation with 8452164 free bytes and 8MB until OOM error.
这是一个数组文件,列出了可绘制文件夹中需要加载的所有图像名称。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="photo_frames">
<item>pf1</item>
<item>pf2</item>
<item>pf3</item>
<item>pf4</item>
<item>pf5</item>
<item>pf6</item>
<item>pf7</item>
<item>pf8</item>
<item>pf9</item>
<item>pf10</item>
<item>pf11</item>
<item>pf12</item>
</string-array>
</resources>
这是 listAdapter 加载图像的代码,photoFrames 是一个保存图像名称的数组。
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//Layout inflate for list item
convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_photo_frames, null);
}
ImageView imgPhotoFrames = (ImageView) convertView.findViewById(R.id.image_photo_frames);
imgPhotoFrames.setImageResource(mContext.getResources().getIdentifier(photoFrames[position], "drawable", mContext.getPackageName()));
return convertView;
}
如果我有几张图片,它可以正常加载,但有点滞后。当有更多图片要加载到 imageView 时,它会返回错误,谢谢。
【问题讨论】:
-
使用 picasso、glide 等图片加载库
-
尝试使用 RecyclerView 并使用诸如Glide 之类的第三方库来加载图像。
标签: android performance android-imageview android-drawable