1

SCENARIO : I am trying to create a slideshow using viewflipper. What I am doing is setting images resource id in an integer array then I use this array to add dynamic ImageViews in viewflipper. Then I use this view flipper as a slideshow on my login page.

PROBLEM : Now the problem is that when I try to add more then 10 images in the array the application wont run and it show OutOfMemory Exception

What should I to do to add more images without this error?

Right now I am adding images from Resource folder but later I will get images link then I have to first download them and then add them to array.

Here is my code :

// Image resource array
int gallery_grid_Images[] = {
        R.drawable.m1, R.drawable.m2, R.drawable.m3, R.drawable.m4, R.drawable.m5, R.drawable.m6, R.drawable.m7, R.drawable.m8, R.drawable.m9, R.drawable.m10, R.drawable.m11, R.drawable.m12,
        R.drawable.m13, R.drawable.m14, R.drawable.m15, R.drawable.m16,
};

// Add child to view flipper
    for (int i = 0; i < gallery_grid_Images.length; i++) {
        setFlipperImage(gallery_grid_Images[i]);
    }

private void setFlipperImage(int res) {
    Log.i("Set Filpper Called", res + "");
    ImageView image = new ImageView(getApplicationContext());
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    image.setLayoutParams(lp);
    image.setAdjustViewBounds(true);
    image.setBackgroundResource(res);
    viewFlip_Slide_Login.addView(image);
}

ERROR LOG

10-27 13:00:43.159: E/dalvikvm-heap(7071): Out of memory on a 7259056-byte allocation.
10-27 13:00:43.159: I/dalvikvm(7071): "main" prio=5 tid=1 RUNNABLE
10-27 13:00:43.159: I/dalvikvm(7071):   | group="main" sCount=0 dsCount=0 obj=0x41d5b568 self=0x41d4b9f8
10-27 13:00:43.159: I/dalvikvm(7071):   | sysTid=7071 nice=0 sched=0/0 cgrp=apps handle=1075398192
10-27 13:00:43.159: I/dalvikvm(7071):   | schedstat=( 2024369000 235624000 595 ) utm=188 stm=14 core=2
10-27 13:00:43.159: I/dalvikvm(7071):   at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.content.res.Resources.loadDrawable(Resources.java:1930)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.content.res.Resources.getDrawable(Resources.java:659)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.view.View.setBackgroundResource(View.java:14179)
10-27 13:00:43.159: I/dalvikvm(7071):   at genie.android.LoginActivity.setFlipperImage(LoginActivity.java:257)
10-27 13:00:43.159: I/dalvikvm(7071):   at genie.android.LoginActivity.onCreate(LoginActivity.java:80)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.Activity.performCreate(Activity.java:5008)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.ActivityThread.access$600(ActivityThread.java:130)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.os.Looper.loop(Looper.java:137)
10-27 13:00:43.159: I/dalvikvm(7071):   at android.app.ActivityThread.main(ActivityThread.java:4745)
10-27 13:00:43.159: I/dalvikvm(7071):   at java.lang.reflect.Method.invokeNative(Native Method)
10-27 13:00:43.159: I/dalvikvm(7071):   at java.lang.reflect.Method.invoke(Method.java:511)
10-27 13:00:43.159: I/dalvikvm(7071):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-27 13:00:43.159: I/dalvikvm(7071):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-27 13:00:43.159: I/dalvikvm(7071):   at dalvik.system.NativeStart.main(Native Method)
10-27 13:00:43.159: A/libc(7071): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 7071 (genie.android)
4

3 回答 3

3

这可能是因为您使用的图像。我的建议:减小图像的大小。如何??试试这些:

  1. 降低分辨率。700-800 像素的高度应该是您的极限。
  2. 使用图像压缩器。我最喜欢的: http: //tinypng.org/

您可以找到大量软件或在线工具来减小图像大小。让我们看看这是否有帮助。

于 2012-10-27T07:41:50.473 回答
0

那是由于您正在使用的图像..由于可绘制图像它会产生位图错误..请参考这个这个这个

您使用的图像ViewFlipper将在您刷了两页后销毁(这取决于您使用 setoffscreenpagelimit 设置了多少,默认情况下我猜它是 1)并且它会重新生成视图但内存引用不会为 GC 祈祷(垃圾收集器)因此您的堆内存最终将继续增加。因此它会导致堆增长..您检查 MAT(内存分析器工具)在那里您可以清楚地检查内存泄漏。

于 2012-10-27T07:35:48.473 回答
0

我不知道您是否解决了这个问题,但使用您system.gc()onDestroy()活动方法来绘制许多可绘制对象。我在打开新活动时遇到了同样的错误。这解决了我的问题。

于 2013-02-23T00:19:51.977 回答