1

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>
Knacks
  • 37
  • 6
  • wait... you've got 70 activities? with one Image and two buttons every single one of them? – Bartek Lipinski Dec 18 '14 at 16:35
  • Yes, I did it that way. Why? – Knacks Dec 18 '14 at 18:42
  • Basically its not really elegant and efficient approach. Activities are heavy and complicated application components, that both with views represent context presented to the user. In your case you could probably do with a single activity and present those gallery images with fragments or just views. Additionally if you want your application to look really good you should think of transitions between such elements and transitions between activities can be unreliable. – Bartek Lipinski Dec 19 '14 at 01:03

3 Answers3

2

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.

Bracadabra
  • 3,609
  • 3
  • 26
  • 46
  • I don't understand that really. Have you got a link where this is described? – Knacks Dec 18 '14 at 15:02
  • When you call startActivity(i); you start another copy of your activity, it means that that you store all your previous activies with bitmaps etc in memory. You should call finish to destroy your activity and free resources. – Bracadabra Dec 18 '14 at 15:04
  • ok, so I have to finish every activity. Is it s.th. with onDestroy? – Knacks Dec 18 '14 at 15:09
  • I know you didn't want to change your current implementation, but it will be easier with ViewPager – Hugo Gresse Dec 18 '14 at 15:10
  • No, onDestory will not called if you don't finish activity, finish it after starting another one or just change picture in current activity – Bracadabra Dec 18 '14 at 15:14
  • But now I want to do it like that. Do I only have to add finish(); after start.activity(i); ? – Knacks Dec 18 '14 at 15:14
  • Just do it, it you try nothing bad happens – Bracadabra Dec 18 '14 at 15:16
  • Then you did smth wrong. You start Picture2 activity but do you finish it or only Picture1 activity? – Bracadabra Dec 18 '14 at 16:00
  • But you don't show picture0 and picture2 activities – Bracadabra Dec 18 '14 at 18:20
  • It's the same code in Picture0 and Picture2 Activity. only that I refer with the intent-Code in picture0-activity to the menue and Picture 1. And in picture2 activity to Menue, Picture1 and Picture3. But in every intent I wrote the finish(); as above. Is that wrong? – Knacks Dec 18 '14 at 18:31
  • No, it looks fine. Well, then there is another problem in your code somewhere. – Bracadabra Dec 18 '14 at 19:39
1

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>
Josef
  • 442
  • 7
  • 16
0

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.

cgew85
  • 427
  • 3
  • 9