'OutOfMemoryError'在ViewFlipper中将多个图像添加到ImageViews时?



我有一个片段包含6个ViewFlipper,每个flipper至少包含10个ImageViews。我试着加载所有的图像并将它们放在ImageViews中,但受限的内存不允许我这样做。每次运行应用程序时,都会发生此异常。

当我减少应用程序运行的图像数量时,但这不是一个解决方案
那么,最好的方法是什么呢?

以下是我迄今为止尝试的内容:

for(int i=0; i<flippers.length; i++) {
    for (int j = 0; j < names.length; j++) {
        int imageResource = getResources().getIdentifier(names[j], "drawable", rootView.getContext().getPackageName());
        ImageView imageView = new ImageView(rootView.getContext());
        imageView.setImageResource(imageResource);
        flippers[i].addView(imageView);
    }
}

26546-26546/com.azad E/AndroidRuntime﹕致命异常:主要的流程:com.azad,PID:26546java.lang.OutOfMemoryError

在加载之前使用库调整图像大小

你可以使用的一些库是

Picasso

UIL

此库将帮助您缓存图像。当不需要图像时,您可以从内存中清除

最新更新