From e704a41477b85b6bb76dff4181df132176aec112 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Sun, 10 Dec 2017 18:47:35 +0100 Subject: [PATCH] Auto cache cleaning + better err in ImageUtilsAnd --- src/be/nikiroo/utils/Cache.java | 7 +++++- .../utils/android/ImageUtilsAndroid.java | 25 ++++++++++++++++--- src/be/nikiroo/utils/ui/ImageUtilsAwt.java | 2 +- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/be/nikiroo/utils/Cache.java b/src/be/nikiroo/utils/Cache.java index 2b32d78d..bba88bb4 100644 --- a/src/be/nikiroo/utils/Cache.java +++ b/src/be/nikiroo/utils/Cache.java @@ -135,7 +135,12 @@ public class Cache { */ private boolean check(File cached, boolean allowTooOld, boolean stable) { if (cached.exists() && cached.isFile()) { - if (allowTooOld || !isOld(cached, stable)) { + if (!allowTooOld && isOld(cached, stable)) { + if (!cached.delete()) { + tracer.error("Cannot delete temporary file: " + + cached.getAbsolutePath()); + } + } else { return true; } } diff --git a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java index 483c44ff..b2359e66 100644 --- a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java +++ b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java @@ -2,15 +2,17 @@ package be.nikiroo.utils.android; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import be.nikiroo.utils.Image; -import be.nikiroo.utils.ImageUtils; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import be.nikiroo.utils.Image; +import be.nikiroo.utils.ImageUtils; + /** - * This class offer some utilities based around images and uses the Android framework. + * This class offer some utilities based around images and uses the Android + * framework. * * @author niki */ @@ -62,7 +64,22 @@ public class ImageUtilsAndroid extends ImageUtils { Bitmap image = BitmapFactory.decodeByteArray(img.getData(), 0, img.getData().length); if (image == null) { - throw new IOException("Failed to convert input to image"); + int size = img.getData().length; + String ssize = size + " byte"; + if (size > 1) { + ssize = size + " bytes"; + if (size >= 1000) { + size = size / 1000; + ssize = size + " kb"; + if (size > 1000) { + size = size / 1000; + ssize = size + " MB"; + } + } + } + + throw new IOException( + "Failed to convert input to image, size was: " + ssize); } return image; diff --git a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java index 26d14aab..0c69dc44 100644 --- a/src/be/nikiroo/utils/ui/ImageUtilsAwt.java +++ b/src/be/nikiroo/utils/ui/ImageUtilsAwt.java @@ -61,7 +61,7 @@ public class ImageUtilsAwt extends ImageUtils { * @throws IOException * in case of IO error */ - static public BufferedImage fromImage(Image img) throws IOException { + public static BufferedImage fromImage(Image img) throws IOException { InputStream in = new ByteArrayInputStream(img.getData()); int orientation; -- 2.27.0