简体   繁体   中英

How can I clear Imageview

On my activities there are very many images and after a while the app crashes (out of memory error). So i wanted to clear the images if i close the activity but it didnt work. My code:

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

public class Jubel extends AppCompatActivity {
    public Spinner spinner1;
    public String[] spinnertext = {"Laufbewegungen", "Abschlussbewegungen (1/2)", "Abschlussbewegungen (2/2)", "EAS FC - Freischaltbare Objekte"};
    public ImageView imageView12;
    public ImageView imageView8;
    public ImageView imageView16;
    public ImageView imageView17;
    public ImageView imageView18;
    public ImageView imageView20;
    public ImageView imageView21;
    public ImageView imageView15;
    public ImageView imageView13;
    public ImageView imageView22;
    public ImageView imageView19;
    public ImageView imageView23;
    public ImageView imageView24;
    public ImageView imageView25;
    public ImageView imageView26;
    public ImageView imageView27;
    public ImageView imageView29;
    public ImageView imageView28;
    public ImageView imageView30;
    public ImageView imageView31;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jubel);

        imageView12 = (ImageView) findViewById(R.id.imageView12);
        imageView12.setImageResource(R.drawable.thumbsuck2);
        imageView8 = (ImageView) findViewById(R.id.imageView8);
        imageView8.setImageResource(R.drawable.onearmraised1);
        imageView16 = (ImageView) findViewById(R.id.imageView16);
        imageView16.setImageResource(R.drawable.wristflick6);
        imageView17 = (ImageView) findViewById(R.id.imageView17);
        imageView17.setImageResource(R.drawable.aeroplane7);
        imageView18 = (ImageView) findViewById(R.id.imageView18);
        imageView18.setImageResource(R.drawable.pointtosky8);
        imageView20 = (ImageView) findViewById(R.id.imageView20);
        imageView20.setImageResource(R.drawable.canyouhearme11);
        imageView21 = (ImageView) findViewById(R.id.imageView21);
        imageView21.setImageResource(R.drawable.handsout12);
        imageView15 = (ImageView) findViewById(R.id.imageView15);
        imageView15.setImageResource(R.drawable.armsout5);
        imageView13 = (ImageView) findViewById(R.id.imageView13);
        imageView13.setImageResource(R.drawable.eartwist3);
        imageView22 = (ImageView) findViewById(R.id.imageView22);
        imageView22.setImageResource(R.drawable.fingerpoint4);
        imageView19 = (ImageView) findViewById(R.id.imageView19);
        imageView19.setImageResource(R.drawable.telephone10);
        imageView23 = (ImageView) findViewById(R.id.imageView23);
        imageView23.setImageResource(R.drawable.shhhhh9);
        imageView24 = (ImageView) findViewById(R.id.imageView24);
        imageView24.setImageResource(R.drawable.comeon13);
        imageView25 = (ImageView) findViewById(R.id.imageView25);
        imageView25.setImageResource(R.drawable.doublearmswing15);
        imageView26 = (ImageView) findViewById(R.id.imageView26);
        imageView26.setImageResource(R.drawable.flyingbird16);
        imageView27 = (ImageView) findViewById(R.id.imageView27);
        imageView27.setImageResource(R.drawable.handonhead17);
        imageView29 = (ImageView) findViewById(R.id.imageView29);
        imageView29.setImageResource(R.drawable.armspointingup19);
        imageView28 = (ImageView) findViewById(R.id.imageView28);
        imageView28.setImageResource(R.drawable.heartsymbol18);
        imageView30 = (ImageView) findViewById(R.id.imageView30);
        imageView30.setImageResource(R.drawable.windmill20);
        imageView31 = (ImageView) findViewById(R.id.imageView31);
        imageView31.setImageResource(R.drawable.blowkisses14);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> spinneradapter = new ArrayAdapter<String>(Jubel.this, android.R.layout.simple_spinner_dropdown_item, spinnertext);
        spinner1.setAdapter(spinneradapter);
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                int spinnerposition = spinner1.getSelectedItemPosition();

                switch (spinnerposition) {

                    case 0: {
                        break;
                    }

                    case 1: {
                        finish();
                        Intent intent = new Intent(Jubel.this,JubelAbschluss.class);
                        startActivity(intent);

                        break;
                    }

                    case 2: {
                        finish();
                        Intent intent = new Intent(Jubel.this,JubelAbschluss2.class);
                        startActivity(intent);

                        break;
                    }

                    case 3: {
                        finish();
                        Intent intent = new Intent(Jubel.this,JubelEAS.class);
                        startActivity(intent);

                        break;

                    }
                }
            }
                    @Override
                    public void onNothingSelected (AdapterView < ? > parent){

                    }
                } );}





    public void ToHome(View view) {
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onStop() {
       ((BitmapDrawable)imageView12.getDrawable()).getBitmap().recycle();
        imageView12.setImageDrawable(null);
        ((BitmapDrawable)imageView8.getDrawable()).getBitmap().recycle();
        imageView8.setImageDrawable(null);
        ((BitmapDrawable)imageView16.getDrawable()).getBitmap().recycle();
        imageView16.setImageDrawable(null);
        ((BitmapDrawable)imageView17.getDrawable()).getBitmap().recycle();
        imageView17.setImageDrawable(null);
        ((BitmapDrawable)imageView18.getDrawable()).getBitmap().recycle();
        imageView18.setImageDrawable(null);
        ((BitmapDrawable)imageView20.getDrawable()).getBitmap().recycle();
        imageView20.setImageDrawable(null);
        ((BitmapDrawable)imageView21.getDrawable()).getBitmap().recycle();
        imageView21.setImageDrawable(null);
        ((BitmapDrawable)imageView15.getDrawable()).getBitmap().recycle();
        imageView15.setImageDrawable(null);
        ((BitmapDrawable)imageView13.getDrawable()).getBitmap().recycle();
        imageView13.setImageDrawable(null);
        ((BitmapDrawable)imageView22.getDrawable()).getBitmap().recycle();
        imageView22.setImageDrawable(null);
        ((BitmapDrawable)imageView19.getDrawable()).getBitmap().recycle();
        imageView19.setImageDrawable(null);
        ((BitmapDrawable)imageView23.getDrawable()).getBitmap().recycle();
        imageView23.setImageDrawable(null);
        ((BitmapDrawable)imageView24.getDrawable()).getBitmap().recycle();
        imageView24.setImageDrawable(null);
        ((BitmapDrawable)imageView25.getDrawable()).getBitmap().recycle();
        imageView25.setImageDrawable(null);
        ((BitmapDrawable)imageView26.getDrawable()).getBitmap().recycle();
        imageView26.setImageDrawable(null);
        ((BitmapDrawable)imageView27.getDrawable()).getBitmap().recycle();
        imageView27.setImageDrawable(null);
        ((BitmapDrawable)imageView28.getDrawable()).getBitmap().recycle();
        imageView28.setImageDrawable(null);
        ((BitmapDrawable)imageView29.getDrawable()).getBitmap().recycle();
        imageView29.setImageDrawable(null);
        ((BitmapDrawable)imageView30.getDrawable()).getBitmap().recycle();
        imageView30.setImageDrawable(null);
        ((BitmapDrawable)imageView31.getDrawable()).getBitmap().recycle();
        imageView31.setImageDrawable(null);
        super.onStop();
    }
}

You might want to try using Glide .

It will handle recycling Bitmaps for the most part for you, which takes care of a lot of memory issues you'll run into when making an app with images.

However, it's quite possible that your layout design is the main culprit and you might need to think about showing less images on the screen at once, or using lower resolution images.

Hope this helps.

In you code try to change

@Override public void onStop(){ ..............

....................................... .................................. }

to

@Override

public void onFinish(){ .......................... }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM