我实现了一个搜索文本为我的名单,搜索的文字和图片的内存溢出异常
每个列表行项目包括文本和图像,我从服务器上下载。
我alsways保持两个列表,一个是文本和图像等一个是文本和图像的排序列表的原始列表...
如果我的列表containes只有4项,所以我的应用效果很好,但是如果我的列表containes 20个项目,每个图像为1MB,我需要保持两个列表listImagesOriginal,适配器(= 40MB)内image_array我收到了一个内存溢出异常。
我怎样才能减少内存消费,我不会recive一个内存溢出异常?
非常感谢
我如何能减少
private MyCustomAdapter adapter;
private List<String> text = new ArrayList<String>();
private List<Bitmap> listImages;
private List<String> textOriginal = new ArrayList<String>();
private List<Bitmap> listImagesOriginal =new ArrayList<Bitmap>();
adapter =new MyCustomAdapter(text,listImages);
ListView.setAdapter(adapter);
searchText= (EditText) findViewById(R.id.profile_page_search);
searchText.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// Abstract Method of TextWatcher Interface.
textlength = searchText.getText().length();
adapter.deleteAllForSearch();
for (int i = 0; i < textOriginal.size(); i++)
{
if(textOriginal.get(i).contains(
searchText.getText().toString().trim()))
{
adapter.addObjectForSearch(textOriginal.get(i), listImagesOriginal.get(i));
}
}
}
public void beforeTextChanged(CharSequence s,
int start, int count, int after)
{
// Abstract Method of TextWatcher Interface.
}
public void onTextChanged(CharSequence s,
int start, int before, int count)
{
}
});
class MyCustomAdapter extends BaseAdapter{
public List<String> text_array = new ArrayList<String>();
public List<Bitmap> image_array = new ArrayList<Bitmap>();
public int getCount(){
return text_array.size();
}
MyCustomAdapter(List<String> text, List<Bitmap>)
{
text_array = text;
image_array = image;
}
public long getItemId(int position){
return position;
}
public String getItem(int position){
return null;
}
public View getView(final int position, View convertView, ViewGroup parent) {
//my implementation
}
public void addObjectForSearch(String text, Bitmap bitmap) {
text_array.add(text);
image_array.add(bitmap);
notifyDataSetChanged();
}
保持图像是不是一个好主意,因为它消耗的内存极大。 (特别是对于应用程序,这可能会降低设备和消耗大量功率的),除非你有一个特别的理由让加载的图像,尝试本地存储的地方的形象和对存储的文件路径,而不是。 – wns349 2013-04-08 14:39:32
谢谢,我如何将图像保存到SD卡?并在我的应用程序退出删除它们? – 2013-04-08 14:46:26