Picasso image is not loading on first run

狂风中的少年 提交于 2020-01-05 03:11:05

问题


I am using picasso to load image from a url. Since i needed bitmap for further processing, I am using Target() class for saving the bitmap. But picasso is not loading the image on the first run. But it loads at the time when i goes to another activity and getting back to the picasso implemented activity. Why it is happening ? Any fixes? My code is below,

 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);
                            }

回答1:


Its a known issue, as picasso only keeps a week reference:

A solution to this issue would be to set the target as a tag to the view component you wish to set.

So your code will look like this:

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);

A proper explanation for the same is given in this SO post!




回答2:


You can use this for loading images.

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

where, carImg is a id of Imageview in XML, carImageUrl is a resource




回答3:


Try this function which I use: And use img.setTag(/*some other object than path of file or errId. But don't forget to add it before using this function*/). If you dont want to use it in same way then remove if conditions on checking getTag().

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);
        }
    }



回答4:


Picasso image load for ViewGroup (RelativeLayout, LinearLayout, FrameLayout etc

In my case following way works.

Need HandlerThread and Handler to load image.

Following example in kotlin. You may convert in Java as required.

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)
            })
        }
    }
})

Hope this would help you.




回答5:


You can try to add placeholder property to picasso:

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

Hope helpful for you!



来源:https://stackoverflow.com/questions/42826035/picasso-image-is-not-loading-on-first-run

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!