毕加索图像在第一次运行时无法加载

3

我正在使用Picasso从URL加载图像。由于我需要位图进行进一步处理,因此我使用Target()类保存位图。但是,Picasso在第一次运行时没有加载图像。但是当我转到另一个活动并返回到实现了Picasso的活动时,它就会加载。为什么会发生这种情况?有什么解决方法吗?我的代码如下:

 Picasso.with(getActivity()).load(card.getExtras().getImageUrl()).into(new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                            SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
                            Date now = new Date();
                            filename ="certificate_"+ formatter.format(now) + ".png";

                            File path=null;
                            if (getActivity().getExternalCacheDir()==null) {

                               path=getActivity().getCacheDir();
                            }
                            if(getActivity().getExternalCacheDir()!=null){
                                path=getActivity().getExternalCacheDir();
                            }
                           File  image=new  File(path+filename);
                            FileOutputStream fileOutPutStream = null;
                            try {
                                fileOutPutStream = new FileOutputStream(image);
                                bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream);

                                fileOutPutStream.flush();
                                fileOutPutStream.close();
                                Log.d("---REACHED","FILE SAVED--------------");
                            } catch (Exception e) {

                                Crashlytics.logException(e);
                            }

Picasso.with(this) .load("你的图片链接") .into(imageView); - Vanraj Ghed
你的设置ImageView的代码在哪里? - Jaydeep Devda
你应该尝试将图片的加载与保存分开处理,使用Picasso库来加载图片。 - Sammy T
我建议你使用Glide。 - Paranoid
6个回答

5

这是一个已知的问题,因为Picasso只保留了一周的引用:

解决此问题的方法是将目标设置为要设置的视图组件的tag

因此,您的代码将如下所示:

Target target = new Target() {
                        @Override
                        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                           .....
// set the tag to the view
holder.imageView.setTag(target);

//set the target to picasso
Picasso.with(getActivity()).load(card.getExtras().getImageUrl()).into(target);

这个问题的适当解释在这篇SO帖子中给出了!


1

我知道这是一个老问题,但对于那些想知道的人来说。问题是由于目标变量的弱引用导致的,所以只需将目标全局定义,如果您愿意作为类变量,这样就可以解决问题。


0

你可以使用这个来加载图片。

Picasso.with(getActivity()).load(carImageUrl).into(carImg);

其中, carImg是XML中ImageView的ID, carImageUrl是资源


0

针对 ViewGroup (RelativeLayout, LinearLayout, FrameLayout 等的 Picasso 图片加载

在我的情况下,以下方法有效。

需要使用 HandlerThreadHandler 来加载图片。

以下是 Kotlin 的示例。您可以根据需要转换为 Java

val handlerThread = HandlerThread("ImageLoader")
handlerThread.start()

val handler = Handler(handlerThread.looper)
handler.post({
    var bitmap: Bitmap? = null
    try {
        bitmap = Picasso.with(this).load(iamgeUrl).get()
    } catch (e: IOException) {
        e.printStackTrace()
    } finally {
        if (bitmap != null) {
            runOnUiThread({
                imageView.background = BitmapDrawable(resources, bitmap)
            })
        }
    }
})

希望这能对你有所帮助。

0

你可以尝试在Picasso中添加placeholder属性:

Picasso.with(this).load(imageData)
       .placeholder(R.drawable.placeholder)
       .resize(200,200)
       .into(mImageView)

希望对你有所帮助!

0
尝试使用我使用的这个函数: 并使用img.setTag(/ *某些其他对象而不是文件路径或errId。但在使用此函数之前不要忘记添加它* /)。如果您不想以相同的方式使用它,则删除检查getTag()if条件。
public static void setImage(final Context context, final ImageView img, @DrawableRes final int defId,
                                @DrawableRes final int errId, final File file, Picasso.Priority priority) {
        if (null != img.getTag()) {
            if (null == img.getDrawable() || !(img.getTag() instanceof String && (img.getTag().equals(file.getAbsolutePath())))) {
                try {
                    if (file.exists()) {
                        Picasso.with(context.getApplicationContext())
                                .load(file)
                                .priority(priority)
                                .placeholder(defId)
                                .error(errId)
                                .fit()
                                .centerInside()
                                .tag(context)
                                .noFade()
                                .into(img, new Callback() {
                                    @Override
                                    public void onSuccess() {
                                        img.setTag(file.getAbsolutePath());
                                    }

                                    @Override
                                    public void onError() {
                                        img.setTag(errId);
                                    }
                                });
                    } else {
                        img.setImageResource(defId);
                        img.setTag(defId);
                    }

                } catch (Exception e) {
                    img.setImageResource(defId);
                    img.setTag(defId);
                }
            }
        } else {
            img.setImageResource(defId);
            img.setTag(defId);
        }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接