gpt4 book ai didi

Java OutOfMemoryError 使用 Picasso 图像

转载 作者:太空宇宙 更新时间:2023-11-03 13:15:06 24 4
gpt4 key购买 nike

我正在尝试将图像加载到 ImageView 中并收到 OutOfMemoryError。图像根据 ViewPager 中的特定 View 动态显示。但是,在我继续滚动浏览 ViewPager 之后,图像最终无法加载并导致 OutOfMemoryError。

请注意,我在 ViewPager 中有 4 个 View ,应用程序似乎在 View 2 上崩溃。我猜图像的尺寸太大了,但我的印象是 Picasso 会相应地处理:

picasso 代码:

Picasso.with(getActivity())
.load(mImageURL)
.fit()
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_CACHE)
.into((ImageView) rootView.findViewById(R.id.comments_image));

XML

<ImageView
android:id="@+id/comments_image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".525"
android:background="@color/black">
</ImageView>

错误

java.lang.OutOfMemoryError: Failed to allocate a 5074572 byte allocation with 132096 free bytes and 129KB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:812)
at android.graphics.Bitmap.createBitmap(Bitmap.java:789)
at android.graphics.Bitmap.createBitmap(Bitmap.java:756)
at android.graphics.drawable.VectorDrawable$VectorDrawableState.createCachedBitmapIfNeeded(VectorDrawable.java:713)
at android.graphics.drawable.VectorDrawable.draw(VectorDrawable.java:280)
at com.squareup.picasso.PicassoDrawable.draw(PicassoDrawable.java:105)
at android.widget.ImageView.onDraw(ImageView.java:1176)
at android.view.View.draw(View.java:15231)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.support.design.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1131)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.support.v4.view.ViewPager.draw(ViewPager.java:2341)
at android.view.View.updateDisplayListIfDirty(View.java:14167)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1373)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.updateDisplayListIfDirty(View.java:14162)
at android.view.View.getDisplayList(View.java:14189)
at android.view.View.draw(View.java:14959)
at android.view.ViewGroup.drawChild(ViewGroup.java:3405)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198)
at android.view.View.draw(View.java:15234)
at android.widget.FrameLayout.draw(F

最佳答案

我建议你使用Glide ,因为它被设计为在默认情况下在低成本(和最差的硬件)设备上运行良好。

Picasso 和 Glide 的一个很大的区别是 Glide 的默认格式是 RGB_565 而 Picasso 是 ARGB_8888。正如您在下面看到的,完成相同的工作,它比 Picasso 节省 50% 的内存。

Memory cost

不过,如果您不想更改您的库,您可以尝试使用 .resize(int, int) 方法在 Picasso 的构建器上调整图像大小。

关于Java OutOfMemoryError 使用 Picasso 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37714806/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com