使用Picasso下载图像会在缓存中创建不正确的图像,可能的修复?

时间:2017-01-11 13:16:32

标签: java android image picasso

我有一个带有图像URL的Realm数据库,我需要在ExternalCacheDir中下载这些图像。现在这里有问题:假设我有三张图片:ar11.jpgar12.jpgar13.jpg。一旦我的代码被执行,我在缓存目录中获得3个jpg图像,具有上述名称,但是所有三个图像都是最后一个图像的副本,即ar13.jpg,其名称为ar11,{ {1}},ar12

这是我的代码:

ar13

这可能导致这种冗余,以及如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

为每张图片调用目标。

每个图像都以高速缓存的形式写入每个名称。

首先ar11.jpg保存为ar11.jpg, ar12.jpg, ar13.jpg。 然后ar12.jpgar13.jpg

也会发生同样的情况

试试这段代码:

private void downloadImage()
{
    RealmResults<ARDatabase> results = mRealm.where(ARDatabase.class).findAll();

    for(ARDatabase x:results)
    {
        if(!x.getIsDownloaded())
        {
            mdataCollection.add(new DownLoadList(x.getUrlImg(),x.getUid()));
        }
    }

    for(DownLoadList i:mdataCollection)
    {
        Log.e("Link",""+i.getImageUrl());
        Picasso.with(getApplicationContext()).load(i.getImageUrl()).into(getTarget(i));
    }
}

private Target getTarget(DownLoadList downLoadList) {
    Target target = new Target() {
        @Override
        public void onBitmapLoaded(final Bitmap bitmap, Picasso.LoadedFrom from)
        {
            Log.e("PICASSO","SUCCESSFUL");

            new Thread(new Runnable() {
                @Override
                public void run() {

                    File sd = getExternalCacheDir();
                    File folder = new File(sd, "/arproject/");
                    if (!folder.exists()) {
                        if (!folder.mkdir()) {
                            Log.e("ERROR", "Cannot create a directory!");
                        } else {
                            folder.mkdirs();
                        }
                    }

                    //File[] fileName = {new File(folder, "one.jpg"), new File(folder, "two.jpg")};


                    File fileName = new File(folder, downLoadList.getUid().toLowerCase()+".jpg");


                    if (!fileName.exists()) {
                        try {
                            fileName.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    else
                    {

                        try {
                            FileOutputStream outputStream = new FileOutputStream(String.valueOf(fileName));
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                            outputStream.close();

                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }).start();

        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable)
        {
            Log.e("PICASSO","FAILED"+errorDrawable.toString());

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }

    }
    return target;
}