OutOfMemory:回收图像 [英] OutOfMemory: Recycle Images

查看:20
本文介绍了OutOfMemory:回收图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个相册.我有 70 个活动,每个人都有一个图像(jpeg、png).和两个箭头来回走.图像保存在可绘制文件夹中.我知道还有其他方法可以创建相册,但现在我想这样做.由于 OutOfMemory-Error,应用程序在一段时间后崩溃.它并不总是停在同一个点上,但有时在第 47 次活动之后,另一次在 52 次活动之后.如果我将每个图像只减少到 20KB,那么应用程序运行没有问题.但图像模糊.如果图像为 100KB,则它们很清晰,但应用程序会崩溃.有人告诉我,我必须回收图像才不会内存不足.像这样:

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 代码:

<?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>

推荐答案

问题是你没有完成以前的活动,这就是为什么回收位图没有帮助.如果您想继续以这种方式实现图库,您可以在 onStart 中设置图像并在 onStop 中将其删除或覆盖后退按钮,完成并跟踪您的活动并在按下后重新启动它们.

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.

这篇关于OutOfMemory:回收图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆