安卓:java.lang.OutOfMemoryError: [英] Android : java.lang.OutOfMemoryError:

查看:35
本文介绍了安卓:java.lang.OutOfMemoryError:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Android 上开发了一个使用大量图像的应用程序.

I developed an application that uses lots of images on Android.

drawable 文件夹中有很多图像,比如超过 100 个,我正在开发图像动画应用程序.我使用 imageview 来显示 GIF 图像.我已经使用了将 gif 图像拆分为多个 PNG 格式图像的概念,然后使用它.

There are lots of images present in drawable folder say more then 100, I am developing application for animation of images. I used imageview to show GIF images. I have used the concept of Split gif images into multiple PNG format images and then use it.

每次用户进入应用程序时,我都可以看到内存越来越大,直到用户收到java.lang.OutOfMemoryError.

Each time the user enters to the app, I can see the memory growing more and more until user gets the java.lang.OutOfMemoryError.

那么处理大量图像的最佳/正确方法是什么?

So what is the best/correct way to handle many images?

下面是我的代码:

dog_animation.xml

dog_animation.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/DogView"
android:orientation="vertical" >

<ImageView
    android:id="@+id/dog_animation"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.19" />

dog_animation.xml (Drawable 文件夹)

dog_animation.xml (Drawable folder)

<?xml version="1.0" encoding="utf-8"?>

<item
    android:drawable="@drawable/image"
    android:duration="50"/>
<item
    android:drawable="@drawable/image1"
    android:duration="50"/>
<item
    android:drawable="@drawable/image2"
    android:duration="50"/>
<item
    android:drawable="@drawable/image3"
    android:duration="50"/>
<item
    android:drawable="@drawable/image4"
    android:duration="50"/>
<item
    android:drawable="@drawable/image5"
    android:duration="50"/>
<item
    android:drawable="@drawable/image6"
    android:duration="50"/>
<item
    android:drawable="@drawable/image7"
    android:duration="50"/>
<item
    android:drawable="@drawable/image8"
    android:duration="50"/>
<item
    android:drawable="@drawable/image9"
    android:duration="50"/>
<item
    android:drawable="@drawable/image10"
    android:duration="50"/>
<item
    android:drawable="@drawable/image11"
    android:duration="50"/>
<item
    android:drawable="@drawable/image12"
    android:duration="50"/>
<item
    android:drawable="@drawable/image13"
    android:duration="50"/>
<item
    android:drawable="@drawable/image14"
    android:duration="50"/>
<item
    android:drawable="@drawable/image15"
    android:duration="50"/>
<item
    android:drawable="@drawable/image16"
    android:duration="50"/>
<item
    android:drawable="@drawable/image17"
    android:duration="50"/>
<item
    android:drawable="@drawable/image18"
    android:duration="50"/>
<item
    android:drawable="@drawable/image19"
    android:duration="50"/>
<item
    android:drawable="@drawable/image20"
    android:duration="50"/>
<item
    android:drawable="@drawable/image21"
    android:duration="50"/>
<item
    android:drawable="@drawable/image22"
    android:duration="50"/>
<item
    android:drawable="@drawable/image23"
    android:duration="50"/>
<item
    android:drawable="@drawable/image24"
    android:duration="50"/>
<item
    android:drawable="@drawable/image25"
    android:duration="50"/>
<item
    android:drawable="@drawable/image26"
    android:duration="50"/>
<item
    android:drawable="@drawable/image27"
    android:duration="50"/>
<item
    android:drawable="@drawable/image28"
    android:duration="50"/>
<item
    android:drawable="@drawable/image29"
    android:duration="50"/>
<item
    android:drawable="@drawable/image30"
    android:duration="50"/>
<item
    android:drawable="@drawable/image31"
    android:duration="50"/>
<item
    android:drawable="@drawable/image32"
    android:duration="50"/>
<item
    android:drawable="@drawable/image33"
    android:duration="50"/>
<item
    android:drawable="@drawable/image34"
    android:duration="50"/>
<item
    android:drawable="@drawable/image35"
    android:duration="50"/>
<item
    android:drawable="@drawable/image36"
    android:duration="50"/>
<item
    android:drawable="@drawable/image37"
    android:duration="50"/>
<item
    android:drawable="@drawable/image38"
    android:duration="50"/>
<item
    android:drawable="@drawable/image39"
    android:duration="50"/>
<item
    android:drawable="@drawable/image40"
    android:duration="50"/>
<item
    android:drawable="@drawable/image41"
    android:duration="50"/>
<item
    android:drawable="@drawable/image42"
    android:duration="50"/>
<item
    android:drawable="@drawable/image43"
    android:duration="50"/>
<item
    android:drawable="@drawable/image44"
    android:duration="50"/>
<item
    android:drawable="@drawable/image45"
    android:duration="50"/>
<item
    android:drawable="@drawable/image46"
    android:duration="50"/>
<item
    android:drawable="@drawable/image47"
    android:duration="50"/>
<item
    android:drawable="@drawable/image48"
    android:duration="50"/>
<item
    android:drawable="@drawable/image49"
    android:duration="50"/>
<item
    android:drawable="@drawable/image50"
    android:duration="50"/>
<item
    android:drawable="@drawable/image51"
    android:duration="50"/>
<item
    android:drawable="@drawable/image52"
    android:duration="50"/>
<item
    android:drawable="@drawable/image53"
    android:duration="50"/>
<item
    android:drawable="@drawable/image54"
    android:duration="50"/>
<item
    android:drawable="@drawable/image55"
    android:duration="50"/>
<item
    android:drawable="@drawable/image56"
    android:duration="50"/>

Dog_Animation.java

Dog_Animation.java

public class Dog_Animation extends Activity {

Timer timer = new Timer();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.dog_animation);

    ImageView img = (ImageView) findViewById(R.id.dog_animation);
    img.setBackgroundResource(R.drawable.dog_animation);
    AnimationDrawable frameAnimation = (AnimationDrawable) img
            .getBackground();
    frameAnimation.start();

    timer.schedule(new TimerTask() {
        public void run() {
            Intent intent = new Intent(Dog_Animation.this,
                    Man_Animation.class);
            startActivity(intent);
        }
    }, 10000);
}
}

现在的问题是,当我尝试从一个活动移动到另一个具有相同其他图像的活动时,它给了我 java.lang.OutOfMemory 的错误.

Now the problem is when i try to move from one activity to another which has same some other image to animate it's giving me error of java.lang.OutOfMemory.

我尝试了很多不同的解决方案,例如

I have tried with so many different solution like

@Override
protected void onDestroy() {
    super.onDestroy();

    unbindDrawables(findViewById(R.id.DogView));
    System.gc();
}

private void unbindDrawables(View view) {
    if (view.getBackground() != null) {
        view.getBackground().setCallback(null);
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            unbindDrawables(((ViewGroup) view).getChildAt(i));
        }
        ((ViewGroup) view).removeAllViews();
    }
}

等等,但任何解决方案都不适合我.请帮我解决这个问题.甚至我也提到了 http://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/ 这个链接,但没有解决问题.

and so other also but any solution is not working for me. Please help me to solve this issue. even i have referred http://androidactivity.wordpress.com/2011/09/24/solution-for-outofmemoryerror-bitmap-size-exceeds-vm-budget/ this link but not getting solve the issue.

推荐答案

在你的 AndroidManifest.xml 中将这个保留在 application 标签中,像这样添加 largeHeap:

In your AndroidManifest.xml keep this inside application tag, add largeHeap like this:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"/>

这篇关于安卓:java.lang.OutOfMemoryError:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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