Android Picasso Image does not load

后端 未结 16 1701
醉梦人生
醉梦人生 2020-12-10 04:02

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

相关标签:
16条回答
  • 2020-12-10 04:13

    You can turn on Picasso logs using 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.

    0 讨论(0)
  • 2020-12-10 04:13

    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.

    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);
    
    0 讨论(0)
  • 2020-12-10 04:14

    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"/>
    
    0 讨论(0)
  • 2020-12-10 04:15

    You can use Picasso.with(Context).setLoggingEnabled(true) to enable debugging so as to troubleshoot the exact cause . If the log has something like Error 504 try using try using

    android:usesCleartextTraffic="true"

    in Application tag of your manifest. It worked for me

    Answered here

    0 讨论(0)
  • 2020-12-10 04:16

    check Internet permission in manifaest

    <uses-permission android:name="android.permission.INTERNET"/>
    
    0 讨论(0)
  • 2020-12-10 04:17

    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 :

    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. https://github.com/JakeWharton/picasso2-okhttp3-downloader

    0 讨论(0)
提交回复
热议问题