【问题标题】:Picasso Out of Memory Error?毕加索内存不足错误?
【发布时间】:2016-03-15 09:03:48
【问题描述】:

为了将图像加载到列表视图(在我的情况下为 25 kb“个人资料”图片),我昨天绑定了 picasso,以便更快地加载图像并缓存到磁盘。但从那时起,即使我没有打开加载图像的活动,我也会经常出现内存不足的错误。 难道是缓存没有被清除,所以我的应用程序不能再处理任何服务器请求了吗?有人也遇到过这个问题吗?还是我基本上做错了什么?

通常我的应用程序首先加载谷歌地图,标记放置在 JSON 请求给出的特定位置。如果我滑动到另一个位置来加载这些标记,则在尝试从服务器加载 JSON 时会发生内存不足错误。以前从未发生过,只是因为我在我的应用程序中包含了毕加索库。

这是我的列表视图列表项:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<ImageView
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:padding="8dp"
    android:id="@+id/friends_image"
    />

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:layout_toRightOf="@+id/friends_image"
    android:gravity="center_vertical"
    android:id="@+id/list_item_friends_textview" />

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/text_view_id"
/>

【问题讨论】:

  • 你的图片有这个属性吗? :android:adjustViewBounds="true"也试过.fit().centerCrop()
  • 将它添加到你所有的图像视图中并再次测试它,如果它抛出内存不足,请告诉我
  • 还是同样的错误。还有其他建议吗?

标签: android json android-asynctask out-of-memory picasso


【解决方案1】:

使用 ScaleType

    <ImageView
        android:id="@+id/grid_item_image"
        android:layout_width="wrap_content"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_gravity="center"
        android:scaleType="fitXY"
        android:layout_height="100dp"/>

【讨论】:

  • 我应该同时添加 scaleType 和 android:adjustViewBounds="true" 吗?
【解决方案2】:

添加 AndroidManifest.xml 文件

<application
             ....
             ....
             android:largeHeap="true"

 </application>

【讨论】:

  • 网上很多人说这不是解决方案,因为从长远来看它并不能真正解决问题......?
  • 是的,它并没有解决问题,而是给其他正在运行的应用程序带来问题,它告诉系统应用程序需要更多内存,因此系统通过杀死其他后台应用程序来获取内存
猜你喜欢
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 2017-01-27
  • 2019-03-21
  • 1970-01-01
  • 2015-09-20
相关资源
最近更新 更多