Download and Save Images Using Picasso

前端 未结 3 714
轻奢々
轻奢々 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);
    

提交回复
热议问题