場景:我試圖使用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)
請顯示錯誤日誌... –
請參閱更新的問題。 –
什麼是平均。你的圖像大小? –