2017-05-11 37 views
0

當我嘗試添加一些Linearlayouts其中每一個包含TextViewImageView運行的OutOfMemoryError設置ImageView的

一切正常,但應用程序嘗試setImageResource時崩潰。 (的OutOfMemoryError

我的繼承人活動(相關部分):

my_icon是在res文件夾中的可繪製的名字(名字是在每一個循環(名字來源於XML)不同

// looping through all item nodes <item> 
        for (int i = 0; i < nl.getLength(); i++) { 
         Element e = (Element) nl.item(i); 

         String my_icon = parser.getValue(e, "my_icon"); // my_icon child value 
         int my_count = Integer.parseInt(parser.getValue(e, "my_count")); // my_count child value 
         String my_text = parser.getValue(e, "my_text"); // my_text child value 

         //Create Child-Views 
         LinearLayout tempLinearLayout = new LinearLayout(AnleitungActivity.this); 
         linearLayout.addView(tempLinearLayout); 
          tempLinearLayout.setOrientation(LinearLayout.HORIZONTAL); 
          ViewGroup.LayoutParams params = tempLinearLayout.getLayoutParams(); 
          params.width = ViewGroup.LayoutParams.MATCH_PARENT; 
          params.height = 240; 
          tempLinearLayout.setBackgroundColor(ContextCompat.getColor(AnleitungActivity.this, R.color.anleitung_kachel_hintergrundfarbe)); 

          ViewGroup.MarginLayoutParams marginParams = (ViewGroup.MarginLayoutParams) tempLinearLayout.getLayoutParams(); 
          marginParams.setMargins(0,5,0,0); //für die Margin-Werte brauchen wir extra MarginLayoutParams... 
          tempLinearLayout.setLayoutParams(params); //layoutParams setzen 
          tempLinearLayout.setLayoutParams(marginParams); //MarginLayoutParams setzen 

         TextView tv = new TextView(AnleitungActivity.this); 
         tempLinearLayout.addView(tv); 
          LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(70, ViewGroup.LayoutParams.MATCH_PARENT,1f); 
          tv.setLayoutParams(tvParams); 
          tv.setText(transformText(my_text)); 
          tv.setTextSize(17); 
          tv.setPadding(5,0,0,0); 
          tv.setTextColor(ContextCompat.getColor(AnleitungActivity.this, R.color.anleitung_text_farbe)); 

         ImageView imageView = new ImageView(AnleitungActivity.this); 
         tempLinearLayout.addView(imageView); 
          imageView.getLayoutParams().height = dp2px(getResources(), 60); 
          imageView.getLayoutParams().width = dp2px(getResources(), 60); 
          imageView.setImageResource(getDrawableRessorceIdByName(AnleitungActivity.this, removeFileExtensionFromString(my_icon))); 



        } 

幫助方法

public String removeFileExtensionFromString(String str){ 
    return str.substring(0, str.indexOf(".")); 
} 
+0

你可以找到這個問題在這裏:http://stackoverflow.com/questions/33279223/outofmemory-while-setting-image-in-imageview –

+0

解決...圖像是太大了......只是減少尺寸。 – skm

+0

我不確定這個方法是「getDrawableRessorceIdByName」,你可以使用這個方法來代替「Resources.getIdentifier」,或者你可能已經在使用它了。 – vsatkh

回答

0

有兩種方法來解決這個問題。

1.Try以減少圖像的大小[強烈推薦]

  • 您可以訪問tinyJPG.com縮小尺寸,並保持圖像的質量。

  • 確保你沒有使用非常高分辨率的圖像作爲 他們沒有用。

2.使用largeHeap [不推薦]

  • 添加largeHeap = 「真」 在你的清單

  • 這可能會降低應用程序的性能。