Adding images dynamically to a layout causes memory out of error
<p>I have some 40 small images and I want to add those to a layout dynamically.But it results memory error in some devices.</p>

<p>Below is my snippet for adding images to a layout when a button is LinearLayout tables = </p>

<pre><code>LinearLayout tables=  (LinearLayout) findViewById(R.id.table);
    undo.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {



            for(int i = 0; i &lt; data.length; i++){
                ImageView image = new ImageView(getApplicationContext());
                try{
                    int imgID = getResources().getIdentifier(data[i], "drawable", "package");
                    image.setImageResource(imgID);

                }catch(Exception e){
                    int imgID = getResources().getIdentifier("nia", "drawable", "package");
                    image.setImageResource(imgID);
                }               
                tables.addView(image); 
            }
        }
    });
</code></pre>

<p>can anyone suggest me what I need to do in order to avoid the memory error??</p>
