安卓 Picasso 图片无法加载

18

我加载图片有两种情况,第一种是直接从互联网加载,第二种是加载在设备中下载的图片。每次加载时,有8-9张图片可以显示,1-2张无法显示。我看到decode返回了false,尽力通过Google搜索,但没有找到解决方法。

  1. WAIT_FOR_CONCURRENT_GC阻塞了22ms
  2. WAIT_FOR_CONCURRENT_GC阻塞了20ms
  3. GC_FOR_ALLOC释放了718K,31%可用空间9948K/14256K,暂停了49ms,总计51ms
  4. D / skia:--- decoder-> decode返回falseGC_CONCURRENT释放了1370K,30%可用空间10081K/14256K,暂停了3ms+ 2ms,总计33ms
  5. GC_FOR_ALLOC释放了916K,30%可用空间10029K/14256K,暂停了66ms,总计67ms

这是我使用Picasso加载的代码:

        Picasso.with(activity)
            .load(path)
            .placeholder(R.drawable.thumbnail_placeholder)
            .resize(width,height)
            .into(imageView);
有什么想法来解决这个问题吗?我每次在加载图片到屏幕上时都调用fit()/resize()。非常感谢您的帮助,提前感谢!
顺便说一下,我在两台机器上进行了测试:模拟器和真实设备Samsung Galaxy Tab 3,并且模拟器上没有任何问题,但实际设备上出现了问题。
更新:
问题是由于图像的颜色空间引起的,其中那些未显示的图像就是YMCK颜色空间中的图像。

1
我怀疑这是内存问题,你能否尝试使用较小的图像进行测试? - Jasper van de Klundert
也许图片的URL在你所在的国家被屏蔽了。 - Ayub
18个回答

1
我遇到了同样的错误,我调整了相同的图像大小并将其上传到Firebase,然后使用Picasso加载其URL,这一次它完全正常工作; 图像成功加载。
在调整大小之前,相同的图像未显示,也没有任何Picasso日志。
我花了将近两天的时间才意识到真正的问题是图像大小。 我将我的图像调整为500x500,一切都正常工作。
注意:在我的情况下,我从设备的相机捕获图像,然后将其上传到Firestore,然后使用Picasso加载图像。 因此,为了解决问题,我开始在onActivityResult()方法中调整图像大小,然后将调整大小后的图像保存到本地存储,并将其上传到Firestore。

在我的情况下,我有一个矢量可绘制对象,但Picasso仍然无法加载它。我转而使用了Glide。 - TheRealChx101
1
是的。Glide甚至还有额外的方法,如加载字节数组等。非常适合从数据库中加载而无需手动解码位图。 - TheRealChx101

1
也许这可以帮助某些人,
对于我的url,我使用的是http://localhost:8080/api/get-image/image.jpg...
你需要设置服务器的IP地址,而不是localhost!!

是的,必须输入“IP地址”,而不是本地主机。 - Infomaster

1
请确保您的imageUrl中包含https而不是http 如果您的imageUrl包含http,则需要在res文件夹xml文件夹下创建network_Security_config文件,并将URL注明为E.g
    <<network-security-config>
            <domain-config cleartextTrafficPermitted="true">
                <domain includeSubdomains="true">www.your_domain.com</domain>
            </domain-config>
    </network-security-config>

1

请检查您在清单文件中是否拥有这些权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>

0
没人会像我这样傻,但为了完整起见:如果您在布局文件中向图像视图添加色彩渲染,它将遮盖成功加载的图像。

0
请使用下面的链接。
Picasso.Get().Load(imageUri).Placeholder(Resource.Drawable.patient_profile_pic).Resize(100, 100).Into(imgProflle);

Xamarin或.Net

 byte[] imageBytes;


    using (var webClient = new WebClient())
    {
      imageBytes = webClient.DownloadData(imageUri);
    }

    Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);

    imgProflle.SetImageBitmap(bitmap);

0

我曾面临过相同的问题,但以下是对我有效的解决方案。

implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp:okhttp:2.3.0'
implementation 'com.squareup.okhttp:okhttp-urlconnection:2.3.0'

===============================================================================

private static Picasso picasso = null;
public static Picasso.Builder picassoBuilder ;


public static void loadImageInto(Context context, String url, int width, int 
 height, Target target) {
    initPicasso(context);
  picasso.load(url)
            .error(android.R.drawable.stat_notify_error)
            .resize(width, height)
            .centerCrop()
            .into(target);
}

public static void initPicasso(Context context) {
    if (picasso == null) {
         picassoBuilder = new Picasso.Builder(context);

        try {
            Picasso.setSingletonInstance(picasso);
        } catch (IllegalStateException ignored) {
            // Picasso instance was already set
            // cannot set it after Picasso.with(Context) was already in use
        }
        picassoBuilder.downloader(new OkHttpDownloader(new OkHttpClient())).memoryCache(Cache.NONE).loggingEnabled(true).indicatorsEnabled(true);

        picasso = picassoBuilder.build();
        
    }
}

0
 Picasso.with(activity)
            .load(path)
            .placeholder(R.drawable.thumbnail_placeholder)
            .resize(width,height)
            .into(imageView);

replace it with this below code

 Picasso.get()
            .load(path)
            .placeholder(R.drawable.thumbnail_placeholder)
            .resize(width,height)
            .into(imageView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接