Skip to content Skip to sidebar Skip to footer

Download And Save Images Using Picasso

I'm trying to show my news in a custom ListView. Each news is included of some images and I want to 1.download images from server 2.save in local storage 3.save path of images int

Solution 1:

Try to put Target target definition before call to Picasso.with(this).load(image[i]).into(target);

P.S. Using the following code and I saved images very well. Thanks, anyway.

My Code:

finalStringfileName= mDataset.get(i).getAid() + ".jpg";
        Targettarget=newTarget() {

            @OverridepublicvoidonPrepareLoad(Drawable arg0) {
                return;
            }

            @OverridepublicvoidonBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {

                try {
                    Filefile=null;

                    // judge "imgs/.nomedia"'s existance to judge whether path availableif(LightCache.testFileExist(GlobalConfig.getFirstStoragePath()
                            + "imgs" + File.separator +".nomedia") == true)
                        file = newFile(GlobalConfig.getFirstStoragePath()
                                + "imgs" + File.separator + fileName);

                    elsefile=newFile(GlobalConfig.getSecondStoragePath()
                            + "imgs" + File.separator + fileName);

                    file.createNewFile();
                    FileOutputStreamostream=newFileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
                    ostream.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @OverridepublicvoidonBitmapFailed(Drawable arg0) {
                return;
            }
        };

        Picasso.with(GlobalConfig.getContext())
                .load(Wenku8API.getCoverURL(mDataset.get(i).getAid()))
                .into(target);

Solution 2:

Custom target for storing photo in phone gallery.

publicclassTargetPhoneGalleryimplementsTarget
{
    privatefinal WeakReference<ContentResolver> resolver;
    privatefinal String name;
    privatefinal String desc;

    publicTargetPhoneGallery(ContentResolver r, String name, String desc)
    {
        this.resolver = newWeakReference<ContentResolver>(r);
        this.name = name;
        this.desc = desc;
    }

    @OverridepublicvoidonPrepareLoad(Drawable arg0)
    {
    }

    @OverridepublicvoidonBitmapLoaded(Bitmap bitmap, LoadedFrom arg1)
    {
        ContentResolverr= resolver.get();
        if (r != null)
        {
            MediaStore.Images.Media.insertImage(r, bitmap, name, desc);
        }
    }

    @OverridepublicvoidonBitmapFailed(Drawable arg0)
    {
    }
}

Picasso.with(context).load(image[position]).into(newTargetPhoneGallery(view.getContentResolver(), "image name", "image desc"));

Solution 3:

although this post is old, it seems the question hasn't been answered yet. Reading your code, it appears the call you make to picasso could be asynchronous. You should definitely check that, as if it is the case, you are starting image.length tasks, changing the filename at each new task, leading all tasks to complete and save to the last filename that was set. To solve this, you should override Target constructor and add a filename parameter so it's ready when the task ends, in your onBitmapLoaded listener.

Post a Comment for "Download And Save Images Using Picasso"