Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用毕加索android disck下载图像_Android_Image_Picasso - Fatal编程技术网

使用毕加索android disck下载图像

使用毕加索android disck下载图像,android,image,picasso,Android,Image,Picasso,我使用毕加索库在listview中下载和显示图像,我使用以下代码: Picasso.with(mContext).load(listItem.getMainPhoto()).into(holder.image); 其中listItem.getMainPhoto()是一个web url 但我需要下载服务中的一些图像,通常是在应用程序不工作时,这样用户可以在离线时看到它们,例如,我需要下载10个图像,稍后将在listview中使用 所以我有两个问题: 我如何下载毕加索的图像并将其存储在永久存储器中

我使用毕加索库在listview中下载和显示图像,我使用以下代码:

Picasso.with(mContext).load(listItem.getMainPhoto()).into(holder.image);
其中
listItem.getMainPhoto()
是一个web url

但我需要下载服务中的一些图像,通常是在应用程序不工作时,这样用户可以在离线时看到它们,例如,我需要下载10个图像,稍后将在listview中使用

所以我有两个问题:

  • 我如何下载毕加索的图像并将其存储在永久存储器中,所以当我使用 Picasso.with(mContext).load(listItem.getMainPhoto())到(holder.image)中 库将首先尝试在本地获取图像,如果没有,它将从web获取图像

    2.如果lib已下载永久内存中的图像,如何清理永久内存


    我想这项功能在毕加索是开箱即用的,因为我没有注意到lib有时会显示来自现金的图像。谢谢

    我知道这是一个老问题,但也许有人会发现这很有用

    您可以使用目标下载毕加索的图像:

        Picasso.with(mContext)
        .load(listItem.getMainPhoto())
        .into(target);
    
    private Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from) {
            new Thread(new Runnable() {
                @Override
                public void run() {               
    
                    File file = new File(Environment.getExternalStorageDirectory().getPath() +"/imagename.jpg");
                    try
                    {
                        file.createNewFile();
                        FileOutputStream ostream = new FileOutputStream(file);
                        bitmap.compress(CompressFormat.JPEG, 75, ostream);
                        ostream.close();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
    
                }
            }).start();
        }
        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
        }
    
        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {
            if (placeHolderDrawable != null) {
            }
        }
    };
    
    要清除缓存,可以将此类添加到毕加索软件包中:

    package com.squareup.picasso;
    
    public class PicassoTools {
    
        public static void clearCache (Picasso p) {
            p.cache.clear();
        }
    }