android - 将多个图像添加到 ViewFlipper 中的 ImageViews 时出现“OutOfMemoryError”?

标签 android out-of-memory viewflipper

我有一个包含 6 个 ViewFlippers 的 fragment ,每个 flippers 至少包含 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﹕ FATAL EXCEPTION: main Process: com.azad, PID: 26546 java.lang.OutOfMemoryError

最佳答案

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

您可以使用的一些库是

Picasso

UIL

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

关于android - 将多个图像添加到 ViewFlipper 中的 ImageViews 时出现“OutOfMemoryError”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795082/

相关文章:

android - 在 Android 的 View Flipper 上触摸滚动?

java - ClassLoader 引用未知路径

android - BitmapFactory.decodeStream 内存不足,尽管使用了减少的样本大小

android.graphics.BitmapFactory.nativeDecodeAsset( native 方法)中的 java.lang.OutOfMemoryError

android - 我应该如何解决这个崩溃问题?

android - ViewFlipper - 如何将我的 ImageView 居中

没有动画的Android ViewFlipper翻转

android - VideoView 和渐进式下载

java - 通过 PendingIntent 传递额外内容不起作用

c# - "hill climbing, change max number of threads 5"在我加载数据时以 xamarin 形式在我的 android 设备上调试时