3

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);
                            }
DAA
  • 1,346
  • 2
  • 11
  • 19
AtHul Antony
  • 77
  • 1
  • 11

6 Answers6

5

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!

Community
  • 1
  • 1
MadScientist
  • 2,134
  • 14
  • 27
1

I know this is an old question, but for those who are wondering. The problem is due the weak reference of the target variable, so just define the Target globally, if you prefer as a Class variable and that fixes the issue

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
0

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

Priya Rajan
  • 687
  • 8
  • 21
0

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);
        }
    }
Jimit Patel
  • 4,265
  • 2
  • 34
  • 58
0

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.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
0

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!

TVT. Jake
  • 279
  • 2
  • 7