繁体   English   中英

Android 毕加索图片无法加载

[英]Android Picasso Image does not load

我加载图像有两种情况,一种是直接从互联网上加载图像,另一种是加载设备中下载的图像。 每当我加载时,都会显示 10 个图像中的 8~9 个,而缺少 1-2 个。 我看到解码返回错误,并且尽我所能用谷歌搜索,但无法出现。

  1. WAIT_FOR_CONCURRENT_GC 阻塞了 22 毫秒
  2. WAIT_FOR_CONCURRENT_GC 阻塞了 20 毫秒
  3. GC_FOR_ALLOC 释放 718K,31% 释放 9948K/14256K,暂停 49ms,总共 51ms
  4. D/skia:---解码器->解码返回falseGC_CONCURRENT释放1370K,30%释放10081K/14256K,暂停3ms+2ms,共33ms
  5. GC_FOR_ALLOC 释放 916K,30% 释放 10029K/14256K,暂停 66ms,总共 67ms

这是我用来通过毕加索加载的代码:

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

任何想法如何解决这个问题? 每次将图像加载到屏幕上时,我都会调用 fit()/resize()。 非常感谢帮助,提前致谢!

仅供参考,我在两台机器、模拟器和真机三星 Galaxy Tab 3 上进行了测试,在模拟器上运行没有任何问题,但在真机上出现问题。

更新:

这是由图像的颜色空间引起的,其中未显示的图像是 YMCK 颜色空间中的图像。

您可以使用Picasso.with(Context).setLoggingEnabled(true)打开毕加索日志。 您可能会在那里看到带有原因的错误消息。

还值得记录您正在使用的 URL 并在浏览器中尝试,以防万一。

检查清单中的 Internet 权限

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

在 Picasso 中,您应该在.load()方法中传递 url 以从 Internet 加载图片和File类型的对象以从设备存储加载图片。

因此,如果图片存储在设备上,请像这样加载它:

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

并使用此代码从互联网加载图片:

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

尝试将 URL 开头的“http:”替换为“https:” (如果适用)

(on your String representation of Url).replace("http:", "https:");

为我工作。

不知道它与这个问题是否相关,但我的问题是通过使用 Glide 而不是 Picasso 解决的。

您可以使用Picasso.with(Context).setLoggingEnabled(true)启用调试,以便排查确切原因。 如果日志中有类似Error 504尝试使用 try using

android:usesCleartextTraffic="true"

在清单的应用程序标签中。 它对我有用

在这里回答

如果有什么不起作用,那是因为托管图像的服务器存在一些问题,它们的 url 不会直接将您带到图像,但在后端,其他东西正在运行,它可能会在 chrome 或其他浏览器中打开,但不会在 picasso 中肯定会加载,所以你可以试试这个代码:

final OkHttpClient client = new OkHttpClient.Builder()
        .protocols(Collections.singletonList(Protocol.HTTP_1_1))
        .build();

final Picasso picasso = new Picasso.Builder(this)
        .downloader(new OkHttp3Downloader(client))
        .build();

Picasso.setSingletonInstance(picasso);

OkHttp3Downloader 实例由该库提供。 https://github.com/JakeWharton/picasso2-okhttp3-downloader

看看毕加索:内存不足

检查您是否在 ImageView 中使用固定大小,请参阅@Samuil Yanovski 回答的更多信息

希望这有帮助!!

我遇到了同样的错误,我调整了相同的图像大小并将其上传到 firebase,然后使用 picasso 加载其 URL,这一次它完全正常; 图像已成功加载。

在调整大小之前,没有显示相同的图像,也没有任何毕加索日志。

我花了将近两天的时间才意识到真正的问题在于图像大小。 我将图像大小调整为 500x500,一切正常。

注意:就我而言,我从设备的相机捕获图像,然后将其上传到 firestore,然后使用 picasso 加载图像。 所以为了解决这个问题,我开始在onActivityResult()方法中调整图像大小,然后将调整大小的图像保存到本地存储并将其上传到 firestore。

检查清单中是否有这些权限:

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

也许这有助于某人,

对于我的网址,我使用的是 http:// localhost :8080/api/get-image/image.jpg ...

你需要设置而不是localhost,服务器的ip!!

//首先将链接存储在字符串中

String url =     
        "https://i.pinimg.com/originals/fc/92/13/fc9213e50a1978c845d7195e988a3322.jpg";
        Picasso.with(this).load(url).into(imageView);

希望这个解决方案可以帮助某人

创建 xml 文件名网络并将该代码粘贴到该文件中

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true">
        <trust-anchors>
            <certificates src="system" />
        </trust-anchors>
    </base-config>
</network-security-config>

在你的清单中添加/包含这个network.xml文件和这一行android:usesCleartextTraffic="true"像这样

 <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:networkSecurityConfig="@xml/network"
        android:usesCleartextTraffic="true"

快乐编码,保持安全

确保您的 imageUrl 包含https而不是http如果您的 imageUrl 包含 http 然后在 res 文件夹 xml 文件夹下创建 network_Security_config 文件并提及 URL 例如

    <<network-security-config>
            <domain-config cleartextTrafficPermitted="true">
                <domain includeSubdomains="true">www.your_domain.com</domain>
            </domain-config>
    </network-security-config>
 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);

使用下面的链接

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);

我遇到了同样的问题,但下面是对我有用的解决方案。

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();
        
    }
}

没有人会像我一样愚蠢,但为了完整起见:如果您在布局文件中为图像视图添加色调颜色,它将掩盖成功加载的图像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM