我在我的安卓应用中使用了ViewFlipper。在调用startflipping()后,如果我让应用运行一段时间,很快就会出现以下错误:
11-22 15:36:34.354: ERROR/dalvikvm-heap(428): 307200-byte external allocation too large for this process.
11-22 15:36:34.372: ERROR/(428): VM won't let us allocate 307200 bytes
11-22 15:36:34.375: ERROR/AndroidRuntime(428): Uncaught handler: thread main exiting due to uncaught exception
11-22 15:36:34.384: ERROR/AndroidRuntime(428): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:459)
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:271)
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:296)
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at com.PlayerOrange.ViewPlaylist.populate(ViewPlaylist.java:115)
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at com.PlayerOrange.ViewPlaylist.access$0(ViewPlaylist.java:100)
11-22 15:36:34.384: ERROR/AndroidRuntime(428): at com.PlayerOrange.ViewPlaylist$ProgressTask$1$1.run(ViewPlaylist.java:383)
每30秒我会得到如下关于内存的一些信息:
ActivityManager activityManager = (ActivityManager) getBaseContext().getSystemService(ACTIVITY_SERVICE);
android.app.ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
Log.i(TAG, " memoryInfo.availMem " + memoryInfo.availMem + "\n" );
Log.i(TAG, " memoryInfo.lowMemory " + memoryInfo.lowMemory + "\n" );
Log.i(TAG, " memoryInfo.threshold " + memoryInfo.threshold + "\n" );
在我遇到“发生内存溢出错误导致强制关闭”的情况之前,我在DDMS中看到了以下内容:
11-22 15:36:10.255: INFO/(428): memoryInfo.availMem 50470912
11-22 15:36:10.255: INFO/(428): memoryInfo.lowMemory false
11-22 15:36:10.264: INFO/(428): memoryInfo.threshold 16777216
我不知道如何解决这个错误。
以下是用于填充 ViewFlipper
的代码:
private void populate() {
for (int i = 0; i < jArray.length(); i++) {
System.out.println("lungime" + jArray.length());
LinearLayout l = new LinearLayout(this);
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
l.setBackgroundColor(0x000000);
l.setOrientation(LinearLayout.VERTICAL);
vf.addView(l);
File f = new File(Environment.getExternalStorageDirectory()
+ "/Downloads/");
File[] files = f.listFiles();
Bitmap bitmap = BitmapFactory.decodeFile(files[i].getPath());
img = new ImageView(this);
img.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
img.setImageBitmap(bitmap);
System.out.println("target " + target[i]);
img.setOnTouchListener(this);
img.setId(i);
l.addView(img);
img = null;
}
vf = (ViewFlipper) findViewById(R.id.details);
vf.setFlipInterval(Integer.valueOf(timer) * 1000);
vf.startFlipping();
populate();
我从网上获取图片并保存到SD卡。然后我从SD卡文件夹中获取它们,并将它们添加到viewFlipper中。
欢迎提出任何想法。提前致谢。