public class Main extends Activity
{
int number = 1000;
Drawable[] array;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
array = new BitmapDrawable[number];
for(int i = 0; i <</SPAN> number; i++)
{
Log.e("", "测试第" + (i+1) + "张图片");
array[i] = getResources().getDrawable(R.drawable.img);
}
}
}
输出结果:
04-07 21:49:25.248: D/szipinf(7828): Initializing inflate state
04-07 21:49:25.398: E/(7828): 测试第1张图片
04-07 21:49:25.658: D/dalvikvm(7828): GC_EXTERNAL_ALLOC freed 48K, 50% free 2692K/5379K, external 0K/0K, paused 24ms
04-07 21:49:25.748: E/(7828): 测试第2张图片
04-07 21:49:25.748: E/(7828): 测试第3张图片
………………
………………
04-07 21:49:26.089: E/(7828): 测试第998张图片
04-07 21:49:26.089: E/(7828): 测试第999张图片
04-07 21:49:26.089: E/(7828): 测试第1000张图片
程序没有报错,正常运行,加载1000个Drawable对象没问题。
下面再来看一下加载1000个Bitmap对象的代码,同样的,代码很简单的,我就不解释了!
public class Main extends Activity
{
int number = 1000;
Bitmap bitmap[];
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bitmap = new Bitmap[number];
for (int i = 0; i <</SPAN> number; i++)
{
Log.e("", "测试第" + (i+1) + "张图片");
bitmap[i] = BitmapFactory.decodeResource(getResources(), R.drawable.img);
}
}
}
输出结果:
04-07 22:06:05.344: D/szipinf(7937): Initializing inflate state
04-07 22:06:05.374: E/(7937): 测试第1张图片
04-07 22:06:05.544: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 51K, 50% free 2692K/5379K, external 0K/0K, paused 40ms
04-07 22:06:05.664: E/(7937): 测试第2张图片
04-07 22:06:05.774: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 1K, 50% free 2691K/5379K, external 6026K/7525K, paused 31ms
04-07 22:06:05.834: E/(7937): 测试第3张图片
04-07 22:06:05.934: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 12052K/14100K, paused 24ms
04-07 22:06:06.004: E/(7937): 测试第4张图片
04-07 22:06:06.124: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 18078K/20126K, paused 27ms
04-07 22:06:06.204: E/(7937): 测试第5张图片
04-07 22:06:06.315: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 24104K/26152K, paused 26ms
04-07 22:06:06.395: E/(7937): 测试第6张图片
04-07 22:06:06.495: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 30130K/32178K, paused 22ms
04-07 22:06:06.565: E/(7937): 测试第7张图片
04-07 22:06:06.665: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed <1K, 50% free 2691K/5379K, external 36156K/38204K, paused 22ms
04-07 22:06:06.745: E/(7937): 测试第8张图片
04-07 22:06:06.845: D/dalvikvm(7937): GC_EXTERNAL_ALLOC freed 2K, 51% free 2689K/5379K, external 42182K/44230K, paused 23ms
04-07 22:06:06.845: E/dalvikvm-heap(7937): 6170724-byte external allocation too large for this process.
04-07 22:06:06.885: I/dalvikvm-heap(7937): Clamp target GC heap from 48.239MB to 48.000MB
04-07 22:06:06.885: E/GraphicsJNI(7937): VM won't let us allocate 6170724 bytes
04-07 22:06:06.885: D/dalvikvm(7937): GC_FOR_MALLOC freed <1K, 51% free 2689K/5379K, external 42182K/44230K, paused 25ms
04-07 22:06:06.885: D/AndroidRuntime(7937): Shutting down VM
04-07 22:06:06.885: W/dalvikvm(7937): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-07 22:06:06.885: E/AndroidRuntime(7937): FATAL EXCEPTION: main
04-07 22:06:06.885: E/AndroidRuntime(7937): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.nativeCreate(Native Method)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:349)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.finishDecode(BitmapFactory.java:498)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:473)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:359)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.graphics.BitmapFactory.decodeResource(BitmapFactory.java:385)
04-07 22:06:06.885: E/AndroidRuntime(7937): at bassy.test.drawable.Main.onCreate(Main.java:37)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-07 22:06:06.885: E/AndroidRuntime(7937): at android.app.Activi
补充:移动开发 , Android ,