使用毕加索下载和保存图像 [英] Download and Save Images Using Picasso

查看:83
本文介绍了使用毕加索下载和保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

1.从服务器上下载图像

1.download images from server

2.保存在本地存储中

2.save in local storage

3.将图像路径保存到SQLite

3.save path of images into SQLite

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

4.show images in ListView using my custom adapter.

我在步骤 1 时遇到问题& 2 .我可以从服务器获取新闻并将其显示在我的 ListView

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

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

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

图片在存储中.

请向我建议您的主意...

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

推荐答案

尝试在调用Picasso.with(this).load(image[i]).into(target);

P.S.使用以下代码,我很好地保存了图像.谢谢,还是.

P.S. Using the following code and I saved images very well. Thanks, anyway.

我的代码:

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

这篇关于使用毕加索下载和保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆