2013-04-08 53 views
0

我實現了一個搜索文本爲我的名單,搜索的文字和圖片的內存溢出異常

每個列表行項目包括文本和圖像,我從服務器上下載。

我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(); 

      } 
+1

保持圖像是不是一個好主意,因爲它消耗的內存極大。 (特別是對於應用程序,這可能會降低設備和消耗大量功率的),除非你有一個特別的理由讓加載的圖像,嘗試本地存儲的地方的形象和對存儲的文件路徑,而不是。 – wns349 2013-04-08 14:39:32

+0

謝謝,我如何將圖像保存到SD卡?並在我的應用程序退出刪除它們? – 2013-04-08 14:46:26

回答

0

40MB是太多了機器人。每部手機都有應用程序內存限制,比大多數高端手機使用的要高40MB。您需要實現圖像的磁盤和/或內存緩存。

+0

謝謝,我如何將圖像保存到SD卡?並在我的應用程序退出刪除它們? – 2013-04-08 14:45:10

+0

要保存OT了SD卡,你需要的WRITE_EXTERNAL_STORAGE permissing。然後您可以調用getExternalStorageDirectory來獲取外部驅動器的位置。然後像平常一樣寫入文件。 – 2013-04-08 14:47:46

+0

謝謝,還有,android模擬器會支持將圖像寫入sd? – 2013-04-08 14:48:40

0

你需要使用一個緩存和/或將它們存儲到持久性存儲器(DB,文件系統等),因爲你不希望所有那些在同一時間加載到內存中的位圖,特別是如果你不看他們都在同一時間。許多舊設備的虛擬機堆大小約爲16-24MB,您需要能夠保持合理的內存使用量。裝上內存

http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html