简体   繁体   English

Android 毕加索图片无法加载

[英]Android Picasso Image does not load

There are two situations I load images, first, just directly from the internet, and second, load images that are downloaded in the device.我加载图像有两种情况,一种是直接从互联网上加载图像,另一种是加载设备中下载的图像。 And whenever I load, 8~9 out of 10 images are shown, and 1-2 missing.每当我加载时,都会显示 10 个图像中的 8~9 个,而缺少 1-2 个。 I see that decode returned false, and google'd as hard as I can, but couldn't come up.我看到解码返回错误,并且尽我所能用谷歌搜索,但无法出现。

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

Here's code I use to load through Picasso:这是我用来通过毕加索加载的代码:

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

Any ideas how to solve this issue?任何想法如何解决这个问题? I am calling fit()/resize() every time I get the images to load on the screen.每次将图像加载到屏幕上时,我都会调用 fit()/resize()。 Help much appreciated, thanks in advance!非常感谢帮助,提前致谢!

FYI, I test on both machines, emulator and the real device, Samsung Galaxy Tab 3, and works without any problems on emulator, but problems occur on real device.仅供参考,我在两台机器、模拟器和真机三星 Galaxy Tab 3 上进行了测试,在模拟器上运行没有任何问题,但在真机上出现问题。

UPDATE:更新:

It was causing by image's color space, where images that weren't showing up were the ones that were in YMCK color space.这是由图像的颜色空间引起的,其中未显示的图像是 YMCK 颜色空间中的图像。

You can turn on Picasso logs using Picasso.with(Context).setLoggingEnabled(true) .您可以使用Picasso.with(Context).setLoggingEnabled(true)打开毕加索日志。 You will probably see an error message with a reason there.您可能会在那里看到带有原因的错误消息。

It's also worth logging the URL you are using and trying it a browser, just in case.还值得记录您正在使用的 URL 并在浏览器中尝试,以防万一。

检查清单中的 Internet 权限

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

In Picasso you shoud pass url in .load() method to load picture from internet and object of File type to load picture from device storage.在 Picasso 中,您应该在.load()方法中传递 url 以从 Internet 加载图片和File类型的对象以从设备存储加载图片。

So if the picture is stored on device load it like this:因此,如果图片存储在设备上,请像这样加载它:

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

And use this code to load picture from internet:并使用此代码从互联网加载图片:

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

Try to replace "http:" at the start of your Url with "https:" (if it applies)尝试将 URL 开头的“http:”替换为“https:” (如果适用)

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

Works for me.为我工作。

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

You can use Picasso.with(Context).setLoggingEnabled(true) to enable debugging so as to troubleshoot the exact cause .您可以使用Picasso.with(Context).setLoggingEnabled(true)启用调试,以便排查确切原因。 If the log has something like Error 504 try using try using如果日志中有类似Error 504尝试使用 try using

android:usesCleartextTraffic="true"

in Application tag of your manifest.在清单的应用程序标签中。 It worked for me它对我有用

Answered here 在这里回答

If anything not works, it is because of the some problem with the servers that are hosting images, their url does not directly take you to the image, but in backend something else is working, it may open in chrome or other browser, but not for sure in picasso it will load, so you may try this code :如果有什么不起作用,那是因为托管图像的服务器存在一些问题,它们的 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);

where the OkHttp3Downloader instance is supplied by this library. OkHttp3Downloader 实例由该库提供。 https://github.com/JakeWharton/picasso2-okhttp3-downloader https://github.com/JakeWharton/picasso2-okhttp3-downloader

Take a look of Picasso: out of memory看看毕加索:内存不足

Check that you use fixed size in your ImageView, refer to more info to @Samuil Yanovski answer检查您是否在 ImageView 中使用固定大小,请参阅@Samuil Yanovski 回答的更多信息

Hope this helps!!希望这有帮助!!

I was facing the same error, I resized the same image and uploaded it to firebase then load its URL using picasso and this time it worked totally fine;我遇到了同样的错误,我调整了相同的图像大小并将其上传到 firebase,然后使用 picasso 加载其 URL,这一次它完全正常; image was loaded successfully.图像已成功加载。

Same image was not showing nor there were any picasso logs before it was resized.在调整大小之前,没有显示相同的图像,也没有任何毕加索日志。

It took me almost two days to realise that real issue was with image size.我花了将近两天的时间才意识到真正的问题在于图像大小。 I resized my image to 500x500 and everything worked fine.我将图像大小调整为 500x500,一切正常。

NOTE: In my case I was capturing image from device's camera then uploading it to firestore and then loading image using picasso.注意:就我而言,我从设备的相机捕获图像,然后将其上传到 firestore,然后使用 picasso 加载图像。 So to fix the issue I started to resize image in onActivityResult() method then save resized image to local storage and uploaded it to firestore.所以为了解决这个问题,我开始在onActivityResult()方法中调整图像大小,然后将调整大小的图像保存到本地存储并将其上传到 firestore。

Check if you have these permissions in the manifest:检查清单中是否有这些权限:

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

Maybe this helps someone,也许这有助于某人,

For my url I was using http:// localhost :8080/api/get-image/image.jpg...对于我的网址,我使用的是 http:// localhost :8080/api/get-image/image.jpg ...

You need to set instead of localhost, the server's ip!!你需要设置而不是localhost,服务器的ip!!

//first store the link in string //首先将链接存储在字符串中

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

Hope this solution helps someone希望这个解决方案可以帮助某人

Create xml file name network and paste that code in that file创建 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>

Add/Include this network.xml file and this line android:usesCleartextTraffic="true" in your Manifest like this在你的清单中添加/包含这个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"

Happy coding, Stay safe快乐编码,保持安全

Make sure your imageUrl contains https instead of http If your imageUrl contain http then creat network_Security_config file under res folder xml folder and mention the URL Eg确保您的 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);

Use link below使用下面的链接

Picasso.Get().Load(imageUri).Placeholder(Resource.Drawable.patient_profile_pic).Resize(100, 100).Into(imgProflle);

Xamarin or .Net Xamarin 或 .Net

 byte[] imageBytes;


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

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

    imgProflle.SetImageBitmap(bitmap);

I had faced the same issue but below is the solution which is worked for me.我遇到了同样的问题,但下面是对我有用的解决方案。

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