Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

android - 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...