java.lang.OutOfMemoryError in my android applicati

2019-09-09 09:51发布

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

3条回答
迷人小祖宗
2楼-- · 2019-09-09 10:07

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

查看更多
仙女界的扛把子
3楼-- · 2019-09-09 10:15

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.

查看更多
爷、活的狠高调
4楼-- · 2019-09-09 10:21

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!

查看更多
登录 后发表回答