场景:我试图使用viewflipper创建幻灯片。我正在做的是将图像资源ID设置为一个整数数组,然后我使用这个数组在viewflipper中添加动态ImageViews。然后我在登录页面上使用这个视图脚本作为幻灯片。Android:尝试使用ViewFlipper创建幻灯片时出现内存不足错误
问题:现在的问题是,当我尝试到阵列的应用程序不会运行在添加超过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)
请显示错误日志... –
请参阅更新的问题。 –
什么是平均。你的图像大小? –