Knacks
Knacks

Reputation: 37

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>

Upvotes: 1

Views: 903

Answers (3)

Josef
Josef

Reputation: 452

Add this in your AndroidMainfest.xml file:

android:hardwareAccelerated="false"
android:largeHeap="true"

inside applicaton tag like this:

<application
        android:hardwareAccelerated="false"
        android:largeHeap="true"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Upvotes: 1

cgew85
cgew85

Reputation: 427

First of all you should scale your images down,this decreases the amount of memory used.Here's a good tutorial on that: Loading large bitmaps efficiently

Then you should only have the one picture that is displayed inside your memory. So before starting the new activity recycle your Bitmap and set it to null, the gc will take care of it.

Upvotes: 0

Bracadabra
Bracadabra

Reputation: 3659

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.

Upvotes: 2

Related Questions