0

I am trying to add a few Linearlayouts which every one contains a TextView and an ImageView at runtime.

Everything works but the application crashed when trying to setImageResource. (OutOfMemoryError)

Heres my Activity (relevant part):

my_icon is the name of the drawable in res folder (the name is different in every loop (name comes from 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)));
 }

help method

 public String removeFileExtensionFromString(String str){
 return str.substring(0, str.indexOf("."));
}
asked May 11, 2017 at 14:44
4
  • you can find this question here : stackoverflow.com/questions/33279223/… Commented May 11, 2017 at 14:46
  • Solved...Images were too big...just reduced the sizes. Commented May 11, 2017 at 14:52
  • I am not sure about this method "getDrawableRessorceIdByName", you might to use this method instead "Resources.getIdentifier" or probably you already use it. Commented May 11, 2017 at 14:56
  • It´s working, read my comment before your´s Commented May 11, 2017 at 15:07

1 Answer 1

0

There are two ways to solve this problem.

1.Try to reduce the size of image [ HIGHLY RECOMMENDED]

  • You can visit tinyJPG.com to reduce the size and also maintain the quality of image.

  • Make sure you didn't use very high resolution image as they are not useful.

2.Use largeHeap [NOT RECOMMENDED]

  • Add largeHeap="true" in your Manifest.

  • This may reduce the performance of app a little bit.

answered May 11, 2017 at 15:09
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.