毕加索图像缓存 [英] Picasso image caching

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

问题描述

我想使用Picasso图像缓存下载以下图像下载代码。

I want to download the following image downloading code with Picasso image cache.

DownloadImage downloadImage = new DownloadImage(); 
downloadImage.execute(advert.getImgUrl());

private class DownloadImage extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected Bitmap doInBackground(String... arg) {
        Bitmap bmp = null;
        try {
            URL url = new URL(arg[0]);
            bmp = BitmapFactory.decodeStream(url.openConnection()
                    .getInputStream());

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return bmp;

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        if (result == null) {
            Intent intent = new Intent(AdvertisingActivity.this,
                    AdvertisingErrorActivity.class);
            intent.putExtra("ad-error", "Error downloading image");
        }
        adImg.setImageBitmap(result);
        super.onPostExecute(result);
    }
}

对此我有几个疑问。


  1. 我想并行下载多个图像。如果我用不同的URL值重复调用 Picasso.with(getActivity())。load(url); ,这能完成吗?

我想在一个活动中下载图像,然后在另一个活动中使用它。这可能吗?

I want to download images in one activity and use it in another activity. Is this possible? How can this be done?

如果我呼叫 Picasso.with(getActivity())。load(url); 具有相同的url值不止一次,下载图像后,是否会为随后的调用加载缓存的图像?

If I call Picasso.with(getActivity()).load(url); more than once with the same url value, does this load the cached images for subsequent calls after the image has been downloaded?

如果图像下载过程由于某些原因而无法成功,能否使Picasso向您报告失败?

If the image download process does not succeed for some reasons, can you make Picasso report you of the failure?


推荐答案

我已经对您的问题进行了更多研究,并决定将其发布为答案而不是评论。

I've researched some more into your questions and decided that I should publish this as an answer rather than a comment.


  1. 是-毕加索异步加载图像,因此重复调用将导致并行下载图像。

  2. 是-只需正常进行调用,毕加索将处理已下载内容的重用图片例如在Activity1中,调用 Picasso.with(this).load( image1); ,然后在Activity2中调用相同的URL。图像将已经被缓存(在内存或设备存储中),毕加索将重新使用它,而不是再次下载。

  3. 是的-见上文(Picasso将自动使用缓存的图像)

  4. 这似乎没有明确的答案。您可以做的一件事是提供一张图像以显示在获取真实图像时是否发生错误:

  1. Yes - Picasso loads images asynchronously so making repeated calls will cause images to be downloaded in parallel.
  2. Yes - just make the call as normal and Picasso will handle the re-use of downloaded images e.g. in Activity1, call Picasso.with(this).load("image1"); and, later, make a call to the same URL in Activity2. The image will already be cached (either in memory or on device storage) and Picasso will re-use it, rather than downloading it again.
  3. Yes - see above (Picasso will automatically use cached images where available)
  4. This does not seem to have such a clear-cut answer. One thing you can do is provide an image to display if an error occurs while fetching the real image:

Picasso.with(context)
.load(URL)
.placeholder(R.drawable.user_placeholder_error)
.error(R.drawable.user_placeholder_error)
.into(imageView);

在尝试从网络上获取图像时,将显示占位符;例如,如果URL无效或没有Internet连接,则会显示错误图像。

The 'placeholder' will be displayed whilst the attempt is being made to fetch the image from the web; the 'error' image will be displayed, for instance, if the URL is not valid or if there is no Internet connection.

更新,17/03 / 2014:

Picasso支持使用回调向您报告失败。像这样修改您的常规呼叫(例如上面的示例):

Picasso supports the use of a callback to report you of a failure. Modify your usual call (e.g. the above example) like so:

.into(imageView, new Callback() {
    @Override
    public void onSuccess() {
        // TODO Auto-generated method stub    
    }

    @Override
    public void onError() {
        // TODO Auto-generated method stub
    }
});


最后,听起来像毕加索图书馆的绝佳选择。它肯定使图像下载非常快速且非常容易,所以我非常喜欢它。

In conclusion, it sounds like Picasso would be a great choice of library for you. It definitely makes image downloading very quick and very easy, so I like it a lot.

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

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