更新为 .get() 后,毕加索不再加载图像

Picasso wont load images anymore after updating to .get()

在我迁移到新版本的 Picasso 之前,我的应用程序运行良好,所以 android 工作室告诉我应该将 .with 方法更改为 .get() 所以我更改了它,但现在它不会加载图像,没有给我任何错误,这是我的代码:

Picasso.get().load(image).error(R.drawable.placeholder).placeholder(R.drawable.placeholder).centerCrop().resize(720,720).into(this.image_view_wallpaper_image);

        public static void imageDownload(Context ctx, String url,String name){
            Picasso.get()
                    .load(url)
                    .into(getTarget(url,name,ctx));
        }
        //target to save
        private static Target getTarget(final String url, final String name, final Context ctx){
            Target target = new Target(){

                @Override
                public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
                    new Thread(new Runnable() {

                        @Override
                        public void run() {

                            File file = new File(Environment.getExternalStorageDirectory().toString() + ctx.getResources().getString(R.string.DownloadFolder) + name);
                            try {
                                file.createNewFile();
                                FileOutputStream ostream = new FileOutputStream(file);
                                bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                                ostream.flush();
                                ostream.close();
                            } catch (IOException e) {
                                Log.e("IOException", e.getLocalizedMessage());
                            }
                        }
                    }).start();
                    Log.v("ok","onBitmapLoaded");


                }
    public void loadBitmap(String url) {

            if (loadtarget == null) loadtarget = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    tempBmp=bitmap;
                    new WallpaperOperation().execute("");
                }

                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {

                }



                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            };
            Picasso.get().load(url).into(loadtarget);
        }

在更新之前它是这样的并且运行良好:

Picasso.with(getApplicationContext()).load(image).error(R.drawable.placeholder).placeholder(R.drawable.placeholder).centerCrop().resize(720,720).into(this.image_view_wallpaper_image);

显然这不是完整的代码我只是把我改变的部分放在一起,我没有收到任何错误,毕加索是最新版本但它不会显示任何东西,我也迁移到 android x 也是。

使用 Glide 库代替 Picasso,因为 Glide 比 Picasso 和其他一些功能更快。

dependencies {
implementation 'com.github.bumptech.glide:glide:3.8.0'

}

用法

Glide.with(context)
                    .load(url)
                    .listener(new RequestListener<String, GlideDrawable>() {
                        @Override
                        public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                            holder.mProgressBar.setVisibility(View.GONE);
                            holder.mImgProduct.setImageResource(R.drawable.ic_cart);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                            holder.mProgressBar.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .into(holder.mImgProduct);

根据评论,问题是 http 端点问题。您有两个选择:

选项 1。在传递给 Picasso 之前将您的 url 的 http 替换为 https

    String imageUrl = "http://www.myurl.com/images/earth.png";
    imageUrl = imageUrl.replace("http","https");
    Picasso.get().url(imageUrl).....

选项 2. 使用支持 Http 连接的 http 客户端,例如 OkHttp。要使用它,请将依赖项添加到 gradle.build

implementation("com.squareup.okhttp3:okhttp:4.1.1")