我正在使用图像缓存和位图将图像从本地资源加载到列表适配器但仍然出现内存错误,我搜索了很多答案,但似乎找不到合适的,可以任何人修复我的代码好吗?提前谢谢!
let strigDate="json date"
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
let date=dateFormatter.dateFromString(strigDate)
dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy/mm/dd hh:mm:ss"
let stringNewDate=dateFormatter.stringFromDate(date)
答案 0 :(得分:0)
你试过吗
android:largeHeap = "true"
答案 1 :(得分:0)
在适配器中,您要为列表中的每个项目充气新视图。这可能对性能和内存不利。看起来像viewholder模式可能对你有所帮助。如果缓存中的位图为空,您还应该确保只进行解码工作。
public class WrapsItems extends ListActivity {
public static Integer[] wraps = {
R.drawable.wraps_parisian,
R.drawable.wraps_beiruti_2,
R.drawable.wraps_berlin,
R.drawable.wraps_cyprus_wrap,
R.drawable.wraps_mexican_fajita,
R.drawable.wraps_megabite,
R.drawable.wraps_monaco,
R.drawable.wraps_hail_caesar,
R.drawable.wraps_philly_steak,
R.drawable.wraps_cheezzy_blt,
R.drawable.wraps_britannia,
R.drawable.wraps_ranchero,
R.drawable.wraps_ibiza,
};
public static class ViewHolderItem {
TextView item_name;
TextView item_price;
TextView item_ingredient;
ImageView imageView;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.categories_item_list);
listAdapter();
}
private void listAdapter(){
setListAdapter(new MyAdapter(this,
android.R.layout.simple_list_item_2, R.id.items_name,
getResources().getStringArray(R.array.wraps_names)));
}
private class MyAdapter extends ArrayAdapter<String> {
LruCache<Integer, Bitmap> imageCache;
public MyAdapter(Context context, int resource, int textViewResourceId, String[] strings) {
super(context, resource, textViewResourceId, strings);
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int casheSize = maxMemory / 8;
imageCache = new LruCache<>(casheSize);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItem viewHolder;
String[] names = getResources().getStringArray(R.array.wraps_names);
String[] prices = getResources().getStringArray(R.array.wraps_prices);
String[] ingredients = getResources().getStringArray(R.array.wraps_ingredients);
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.categories_item, parent, false);
viewHolder = new ViewHolderItem();
viewHolder.item_name = (TextView) convertView.findViewById(R.id.items_name);
viewHolder.item_price = (TextView) convertView.findViewById(R.id.item_price);
viewHolder.item_ingredient = (TextView) convertView.findViewById(R.id.ingredients);
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.item_image);
viewHolder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolderItem) convertView.getTag();
}
viewHolder.item_name.setText(names[position]);
viewHolder.item_price.setText(prices[position]);
viewHolder.item_ingredient.setText(ingredients[position]);
Bitmap bitmap = imageCache.get(position);
if (bitmap == null) {
bitmap = BitmapFactory.decodeResource(getResources(), wraps[position]);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
imageCache.put(position, bitmap);
}
viewHolder.imageView.setImageBitmap(bitmap);
return convertView;
}
}
}