【问题标题】:Android Picasso get image bitmap from cache howAndroid Picasso 如何从缓存中获取图像位图
【发布时间】:2018-08-11 20:11:40
【问题描述】:

我没有找到任何关于如何从缓存毕加索中检索位图图像的适当示例:

这是我用毕加索下载图像视图的代码。 我需要位图图像。如何从缓存中取出?

MWGApp.getInjector().getImageLoader().load(url)   
         .placeholder(ResourcesCompat.getDrawable(context.getResources(), R.drawable.image_coming_soon, context.getTheme()))
                .into(imageVoew);
    }

【问题讨论】:

    标签: android picasso


    【解决方案1】:

    从官网上可以看到Picasso有如下特点:

    • 在适配器中处理 ImageView 回收和下载取消。
    • 使用最少的内存进行复杂的图像转换。
    • 自动内存和磁盘缓存。 . .

    要从Picasso 获取Bitmap,您可以设置您的代码:

    Picasso.with(this)
                .load(youUrl)
                .into(new Target() {
                    @Override
                    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
    
                  // here is your bitmap use it
                    }
    
                    @Override
                    public void onBitmapFailed(Drawable errorDrawable) {
    
                    }
    
                    @Override
                    public void onPrepareLoad(Drawable placeHolderDrawable) {
    
                    }
                });
    

    【讨论】:

    • 谢谢,这真的很有帮助。在我们的例子中,我们需要获取位图并应用一些变换并手动将其绘制在画布上。 .get() 方法做到了,但是由于缓存限制(如方法的 javadoc 中所述),它不断地从网络中获取图像。因此, .into() 确实缓存了图像并返回给我们位图。谢谢。
    【解决方案2】:
    public static Bitmap getBitmap(Context context, String url)
    {
        final String CACHE_PATH = context.getCacheDir().getAbsolutePath() + "/picasso-cache/";
    
        File[] files=new File(CACHE_PATH).listFiles();
        for (File file:files)
        {
            String fname= file.getName();
            if (fname.contains(".") && fname.substring(fname.lastIndexOf(".")).equals(".0"))
            {
                try
                {
                    BufferedReader br=new BufferedReader(new FileReader(file));
                    if (br.readLine().equals(url))
                    {
                        String image_path=  CACHE_PATH + fname.replace(".0", ".1");                     
                        if (new File(image_path).exists())
                        {
                            return BitmapFactory.decodeFile(image_path);
                        }
                    }
                }
                catch (FileNotFoundException|IOException e)
                {
                }
            }
        }
        return null;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      相关资源
      最近更新 更多