Auto cache cleaning + better err in ImageUtilsAnd
authorNiki Roo <niki@nikiroo.be>
Sun, 10 Dec 2017 17:47:35 +0000 (18:47 +0100)
committerNiki Roo <niki@nikiroo.be>
Sun, 10 Dec 2017 17:47:35 +0000 (18:47 +0100)
src/be/nikiroo/utils/Cache.java
src/be/nikiroo/utils/android/ImageUtilsAndroid.java
src/be/nikiroo/utils/ui/ImageUtilsAwt.java

index 2b32d78d67c03d20ba94148b8f92b2f7bdabda23..bba88bb4b48f7e970039002393a72a47d50081cf 100644 (file)
@@ -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;
                        }
                }
index 483c44ff144ec8f705412e85c2c9b428e973999f..b2359e66338d9d0d7959b6fae56fa3ea6b0c7b72 100644 (file)
@@ -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;
index 26d14aaba2bf4ec30e30aac10992fa06efc9a468..0c69dc4482938ae02608be704167e23a8add13e0 100644 (file)
@@ -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;