java.lang.OutOfMemoryError in my android application

只愿长相守 提交于 2019-12-24 19:45:25

问题


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

回答1:


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:

Bitmap alpha;
Bitmap foo;

Now initialize the Bitmaps in the onResume():

Options options = new Options();
alpha = BitmapFactory.decodeResource(game.getResources(), R.drawable.youBitmapName, options);
foo = BitmapFactory.decodeResource(game.getResources(), R.drawable.youBitmapName2, options);

Options will give you the ability to downsample. (I'm not sure how big your images are, but you might also want to use the scaling methods then).

In the onPause, clean up resources by calling:

alpha.recycle();
alpha = null;
foo.recycle();
foo = null;

As soon as the onResume() method is called, the bitmaps will reinitialize.




回答2:


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!




回答3:


While creating the emulator, increase the 'VM application heap size' and 'ram' under hardware setting. This will hopefully solve your problem.



来源:https://stackoverflow.com/questions/11447409/java-lang-outofmemoryerror-in-my-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!