情景:我正在尝试使用viewflipper创建幻灯片。我正在做的是在整数数组中设置图像资源ID然后我使用此数组在viewflipper中添加动态ImageViews。然后我在登录页面上使用此视图鳍状肢作为幻灯片显示。
问题:现在的问题是,当我尝试在数组中添加超过10个图像时,应用程序将无法运行并显示OutOfMemory Exception
如果没有此错误,我应该怎么做才能添加更多图片?
现在我正在添加来自Resource folder
的图片,但稍后我会获得图片链接然后我必须首先下载它们然后将它们添加到数组中。
这是我的代码:
// 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);
}
错误日志
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)
答案 0 :(得分:3)
这可能是因为您正在使用的图像。我的建议:缩小图像的大小。怎么样??试试这些:
您可以找到一堆软件或在线工具来减少图像尺寸。让我们看看这是否有帮助。
答案 1 :(得分:0)
这是由于您正在使用的图像..由于可绘制的图像,它会创建位图错误..对于该引用this 和this 和this
你在ViewFlipper
中使用的图像会在刷过两页后消失(这取决于你使用setoffscreenpagelimit设置了多少,默认情况下它是1我猜)并且它会重新生成视图但内存引用不会为GC(垃圾收集器)祈祷,因此你的堆内存最终会继续增加。因此它会导致堆增长..你检查MAT(Memeory Analyzer Tool),你可以清楚地检查内存泄漏。
答案 2 :(得分:0)
我不知道您是否已经解决了这个问题,但在您的活动的system.gc()
方法中使用了onDestroy()
来吸引许多可绘制的内容。打开新活动时我遇到了同样的错误。这解决了我的问题。