Download and Save Images Using Picasso

末鹿安然 提交于 2019-12-03 01:56:14
MewX

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:

        final String fileName = mDataset.get(i).getAid() + ".jpg";
        Target target = new Target() {

            @Override
            public void onPrepareLoad(Drawable arg0) {
                return;
            }

            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom arg1) {

                try {
                    File file = null;

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

                    else file = new File(GlobalConfig.getSecondStoragePath()
                            + "imgs" + File.separator + fileName);

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

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

            @Override
            public void onBitmapFailed(Drawable arg0) {
                return;
            }
        };

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

Custom target for storing photo in phone gallery.

public class TargetPhoneGallery implements Target
{
    private final WeakReference<ContentResolver> resolver;
    private final String name;
    private final String desc;

    public TargetPhoneGallery(ContentResolver r, String name, String desc)
    {
        this.resolver = new WeakReference<ContentResolver>(r);
        this.name = name;
        this.desc = desc;
    }

    @Override
    public void onPrepareLoad (Drawable arg0)
    {
    }

    @Override
    public void onBitmapLoaded (Bitmap bitmap, LoadedFrom arg1)
    {
        ContentResolver r = resolver.get();
        if (r != null)
        {
            MediaStore.Images.Media.insertImage(r, bitmap, name, desc);
        }
    }

    @Override
    public void onBitmapFailed (Drawable arg0)
    {
    }
}

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

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.

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