Download and Save Images Using Picasso

做~自己de王妃 提交于 2019-12-20 11:54:08

问题


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 into SQLite

4.show images in ListView using my custom adapter.

I just have problem with steps 1 & 2. I can get news from server and show them in my ListView

and show images from cache by add below code in my adapter:

Picasso.with(context).load(image[position]).into(iv);

By using Picasso.with(context).load(image[position]).into(target) , just I can save one

image in storage.

Please suggest me your idea ...

UPDATE: When I use below code, just one image (last index of my image array) being saved!

How can I save all images in array with this code?!

@Override
protected void onPostExecute(Void result) {
   SaveImages();
   pDialog.dismiss();
   super.onPostExecute(result);
}

String fileName = null;

public void SaveImages() {
    for(int i = 0; i < image.length; i++) {
        Picasso.with(this).load(image[i]).into(target);
        fileName = "image-" + i + ".jpg";
    }
}

Target target = new Target() {

    @Override
    public void onPrepareLoad(Drawable arg0) {
    }

    @Override
    public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
        File file = new File(Environment.getExternalStorageDirectory().getPath() +"/" + fileName);
         try {
             file.createNewFile();
             FileOutputStream ostream = new FileOutputStream(file);
             bitmap.compress(CompressFormat.JPEG, 75, ostream);
             ostream.close();
         } catch (Exception e) {
             e.printStackTrace();
         }
    }

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

回答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:

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



回答2:


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



回答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.



来源:https://stackoverflow.com/questions/27729976/download-and-save-images-using-picasso

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