首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用毕加索下载和保存图像

使用毕加索下载和保存图像
EN

Stack Overflow用户
提问于 2015-01-01 18:30:01
回答 3查看 30.7K关注 0票数 16

我正在尝试在自定义ListView中显示我的新闻。每个新闻都包含一些图像和我想

1.从服务器下载镜像

2.保存在本地存储中

3.将图像的路径保存到SQLite中

4.使用我的自定义适配器在ListView中显示图像

我只是对steps 1 & 2有问题。我可以从服务器获取新闻,并在我的ListView中显示它们

并通过在适配器中添加以下代码来显示缓存中的图像

代码语言:javascript
复制
Picasso.with(context).load(image[position]).into(iv);

通过使用Picasso.with(context).load(image[position]).into(target),我可以保存一个

存储中的镜像

请给我提一下你的想法。

更新:当我使用下面的代码时,只保存了一个图像(图像数组的最后一个索引)!

如何使用此代码保存数组中的所有图像?!

代码语言:javascript
复制
@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) {
    }
};
EN

回答 3

Stack Overflow用户

发布于 2015-01-23 01:21:28

尝试在调用Picasso.with(this).load(image[i]).into(target);之前放置Target target定义

附注:使用下面的代码,我很好地保存了图像。不管怎样,谢谢你。

我的代码:

代码语言:javascript
复制
        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);
票数 7
EN

Stack Overflow用户

发布于 2015-10-13 00:00:54

用于在电话画廊中存储照片的自定义目标。

代码语言:javascript
复制
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"));
票数 2
EN

Stack Overflow用户

发布于 2017-01-24 16:26:42

虽然这篇文章很老了,但这个问题似乎还没有得到回答。阅读你的代码,你对毕加索的调用可能是异步的。您一定要检查一下,就好像您正在启动image.length任务,更改每个新任务的文件名,引导所有任务完成并保存到设置的最后一个文件名。要解决这个问题,您应该覆盖目标构造函数,并在onBitmapLoaded侦听器中添加一个filename参数,以便在任务结束时准备就绪。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27729976

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档