使用毕加索下载图像会在缓存中创建不正确的图像,可能会解决吗? [英] Downloading images using Picasso creates incorrect images in cache, possible fix?
问题描述
我有一个包含图像URL
的Realm数据库,我需要将这些图像下载到ExternalCacheDir
中.现在,这里是问题:假设我有三个图像:ar11.jpg
,ar12.jpg
,ar13.jpg
.一旦执行了我的代码,我就在缓存目录中获得了3张jpg图像,具有上述名称,但是所有3张图像都是最后一张图像的副本,即ar13.jpg
,其名称为ar11
,ar12
,ar13
.
I have a Realm database with image URL
s, and I need to download these images in the ExternalCacheDir
. Now here lies the problem : assume I have three images : ar11.jpg
,ar12.jpg
,ar13.jpg
. Once my code is executed, I obtain 3 jpg images in the cache dir, with the above mentioned names, however all three images are the replicas of the last image, ie ar13.jpg
with the names ar11
,ar12
,ar13
.
这是我的代码:
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) {
}
};
什么可能导致这种冗余,以及如何解决?
What is probably causing this redundancy, and how to fix it?
推荐答案
为每个图像调用目标.
每个图像都以每个名称写入缓存中.
Each image is getting written in cache under every name.
第一个ar11.jpg
保存为ar11.jpg, ar12.jpg, ar13.jpg
.
然后ar12.jpg
和ar13.jpg
First ar11.jpg
is saved as ar11.jpg, ar12.jpg, ar13.jpg
.
Then same happens with ar12.jpg
and ar13.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;
}
这篇关于使用毕加索下载图像会在缓存中创建不正确的图像,可能会解决吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!