java - 在缓存中,可能修复,使用毕加索下载图像会产生错误的图像?

  显示原文与译文双语对照的内容
113 3

我有一个带有图像 URL的领域数据库,我需要在 ExternalCacheDir 中下载这些图像。 现在是问题: 假设我有三个图像: ar11.jpgar12.jpgar13.jpg 。 代码执行后,在缓存目录中获取 3 jpg图像,上面提到的名称为 last,但是所有三个图像都是最后一个图像的副本,名称为 ar11ar12ar13

这是我的代码:


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


 }


}



 private 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")};



 for (DownLoadList i:mdataCollection)


 {


 File fileName = new File(folder,i.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) {



 }


};



什么是导致这种冗余的原因,以及如何修复它?

时间: 原作者:

106 2

为每个图像调用目标。

每个图像都在每个名字的缓存中写入。

第一个 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;


}



原作者:
...