使用 Picasso 下载图像会在缓存中创建不正确的图像,可能的修复方法?
Downloading images using Picasso creates incorrect images in cache, possible fix?
我有一个包含图像 URL
的 Realm 数据库,我需要在 ExternalCacheDir
中下载这些图像。现在这里是问题:假设我有三张图片:ar11.jpg
、ar12.jpg
、ar13.jpg
。执行我的代码后,我在缓存目录中获得了 3 个 jpg 图像,具有上述名称,但是所有三个图像都是最后一个图像的副本,即 ar13.jpg
,名称为 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) {
}
};
可能是什么导致了这种冗余,如何解决?
为每个图像调用目标。
每个图像都以每个名称写入缓存。
先ar11.jpg
保存为ar11.jpg, ar12.jpg, ar13.jpg
。
然后 ar12.jpg
和 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;
}
我有一个包含图像 URL
的 Realm 数据库,我需要在 ExternalCacheDir
中下载这些图像。现在这里是问题:假设我有三张图片:ar11.jpg
、ar12.jpg
、ar13.jpg
。执行我的代码后,我在缓存目录中获得了 3 个 jpg 图像,具有上述名称,但是所有三个图像都是最后一个图像的副本,即 ar13.jpg
,名称为 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) {
}
};
可能是什么导致了这种冗余,如何解决?
为每个图像调用目标。
每个图像都以每个名称写入缓存。
先ar11.jpg
保存为ar11.jpg, ar12.jpg, ar13.jpg
。
然后 ar12.jpg
和 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;
}