I have 18 images which create a flower when overlap eachother as a stack in my and app. All images are added to activity in XML file android:src:'@drawable/blabla..'
... However when I run my app , it returns an error java.lang.OutOfMemoryError
.Please help me... I dont know how I can solve this problem.Thank you...
public class PapatyaFaliActivity extends Activity {
private int[] startLeafID={R.id.imag,R.id.birstart,R.id.ikistart,R.id.ucstart,R.id.dortstart,R.id.besstart,R.id.altistart,R.id.yedistart,
R.id.sekizstart,R.id.dokuzstart,R.id.onstart,R.id.onbirstart,R.id.onikistart,R.id.onucstart,R.id.ondortstart,R.id.onbesstart,R.id.onaltistart};
private ImageView[] leafstart=new ImageView[17];
private int[] leafResouseID={R.drawable.papatya_orta,R.drawable.leaf_1,R.drawable.leaf_2,R.drawable.leaf_3,R.drawable.leaf_4,R.drawable.leaf_5,R.drawable.leaf_6,
R.drawable.leaf_7,R.drawable.leaf_8,R.drawable.leaf_9,R.drawable.leaf_10,R.drawable.leaf_11,R.drawable.leaf_12,R.drawable.leaf_13,
R.drawable.leaf_14,R.drawable.leaf_15,R.drawable.leaf_16};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
for(int i=0;i<17;i++){
leafstart[i]=(ImageView) findViewById(startLeafID[i]);
}
for(int i=0;i<17;i++){
leafstart[i].setImageResource(leafResouseID[i]);
}
Thread timerThread=new Thread(){
@Override
public void run() {
try {
sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}finally{
Intent menuIntent=new Intent(getBaseContext(),MenuFrame.class);
startActivity(menuIntent);
}
}
};
timerThread.start();
}
@Override
protected void onPause() {
super.onPause();
for(int i=0;i<17;i++){
leafstart[i].setImageResource((Integer) null);
}
finish();
}
}
While creating the emulator, increase the 'VM application heap size' and 'ram' under hardware setting. This will hopefully solve your problem.
Try reading your 18 Bitmaps from within your code. Place the Bitmaps in the res/drawable-hdpi folder. (there are different folders for different image qualities). Set up the Bitmap fields in your code:
Now initialize the Bitmaps in the onResume():
In the onPause, clean up resources by calling:
As soon as the onResume() method is called, the bitmaps will reinitialize.
18 images it is not a big number. If you have OutOfMemoryError this means that your images are simply to big. Simplest and best approach is to adjust (reduce) resolution of those images to the size you really need.
If for some strange reason you need images with hi resolution then you should use miniatures in case if they are shown in small scale, so only one or two images at once are presented in full resolution. To make alternations of drawable item depending of level of details use ImageView.setImageLevel (to change level of details) and define resource as LevelListDrawable.
Calling finish inside onPause is very bad idea!