Download and Save Images Using Picasso

前端 未结 3 728
轻奢々
轻奢々 2021-02-05 11:36

I\'m trying to show my news in a custom ListView. Each news is included of some images and I want to

相关标签:
3条回答
  • 2021-02-05 12:10

    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);
    
    0 讨论(0)
  • 2021-02-05 12:19

    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.

    0 讨论(0)
  • 2021-02-05 12:25

    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"));
    
    0 讨论(0)
提交回复
热议问题