【问题标题】:Picasso image not loading through url in recyclerview毕加索图像未通过 recyclerview 中的 url 加载
【发布时间】:2017-11-18 19:02:30
【问题描述】:

Picasso.with(context).load("http://api.learn2crack.com/android/images/donut.png").resize(218, 192).centerCrop().into(holder.coverImageViewa);

【问题讨论】:

  • 因为链接是从http重定向到https

标签: android image picasso


【解决方案1】:

CustomPicasso.java

import android.content.Context;
import android.util.Log;

import com.jakewharton.picasso.OkHttp3Downloader;
import com.squareup.picasso.Picasso;

/**
 * Created by Hrishikesh Kadam on 19/12/2017
 */

public class CustomPicasso {

    private static String LOG_TAG = CustomPicasso.class.getSimpleName();
    private static boolean hasCustomPicassoSingletonInstanceSet;

    public static Picasso with(Context context) {

        if (hasCustomPicassoSingletonInstanceSet)
            return Picasso.with(context);

        try {
            Picasso.setSingletonInstance(null);
        } catch (IllegalStateException e) {
            Log.w(LOG_TAG, "-> Default singleton instance already present" +
                    " so CustomPicasso singleton cannot be set. Use CustomPicasso.getNewInstance() now.");
            return Picasso.with(context);
        }

        Picasso picasso = new Picasso.Builder(context).
                downloader(new OkHttp3Downloader(context))
                .build();

        Picasso.setSingletonInstance(picasso);
        Log.w(LOG_TAG, "-> CustomPicasso singleton set to Picasso singleton." +
                " In case if you need Picasso singleton in future then use Picasso.Builder()");
        hasCustomPicassoSingletonInstanceSet = true;

        return picasso;
    }

    public static Picasso getNewInstance(Context context) {

        Log.w(LOG_TAG, "-> Do not forget to call customPicasso.shutdown()" +
                " to avoid memory leak");

        return new Picasso.Builder(context).
                downloader(new OkHttp3Downloader(context))
                .build();
    }
}

build.gradle (Module:app)

android {

    ...

}

dependencies {

    ...

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
}

用法-

CustomPicasso.with(context)
    .load("http://api.learn2crack.com/android/images/donut.png")
    .resize(218, 192)
    .centerCrop()
    .into(holder.coverImageViewa);

【讨论】:

    【解决方案2】:

    这是一个网址问题。您使用的网址从 http 重定向到 https,这就是 Picasso 不加载图像的原因。在 url 中使用 https 而不是 http 就可以了。

    https://api.learn2crack.com/android/images/donut.png

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多