Search code examples
androideclipseimagebitmaprecycle

OutOfMemory: Recycle Images


I created a photoalbum. I've got 70 activities and in everyone is one Image (jpeg, png). And two arrows to go Back and forth. The Images are saved in drawable-folder. I know there are other ways to create a photo-album but now I want to make it that way. The app crashes after a time, because of a OutOfMemory-Error. It doesn't always stop on the same Point, but sometimes after the 47th activitiy, a other time after 52 activities. If I reduce every Image only to 20KB then the app runs without Problems. But the Images are blurred. If the Images are 100KB they are sharp then, but the app crashes. Someone told me I have to recycle the Images to be not out of Memory. Like this:

if (bitmap != null) {
    bitmap.recycle();
    bitmap = null;
} 

But I don't know how to do that. This is my code:

     public class Picture1 extends Activity {

            public ImageView iv; 

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.pic1);

               iv = (ImageView)findViewById(R.id.imageView1);}

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

               public void Pic0 (View view){
            Intent i = new Intent(this, Picture0.class);             
            startActivity(i);
            finish();

}

               public void Pic2 (View view){
            Intent i = new Intent(this, Picture2.class);             
            startActivity(i);
            finish();


}}

XML-Code:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

         <ImageView
            android:id="@+id/imageView1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:layout_marginLeft="14dp"
            android:src="@drawable/picture1" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/arrowright" 
            android:onClick="Pic2"/>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="100dp"
            android:layout_height="80dp"
            android:layout_alignTop="@+id/imageView3"
            android:layout_centerHorizontal="true"
            android:onClick="Menue"
            android:src="@drawable/Menue" />

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:src="@drawable/arrowleft" 
            android:onClick="Pic0"/>


         </RelativeLayout>

Solution

  • The problem is that you don't finish previous activities, that's why recycle bitmap doesn't help. If you want to continue implement gallery in this way you can set image in onStart and remove it in onStop or override back button, finish and track your activities and relaunch them when back pressed.