add checks on ImageUtils instances
[nikiroo-utils.git] / src / be / nikiroo / utils / android / ImageUtilsAndroid.java
index 483c44ff144ec8f705412e85c2c9b428e973999f..4b3c1ea631d94689039b225925aff20e4ed57cc0 100644 (file)
@@ -2,19 +2,32 @@ 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 javax.imageio.ImageIO;
+
+import be.nikiroo.utils.Image;
+import be.nikiroo.utils.ImageUtils;
+import be.nikiroo.utils.StringUtils;
+
 /**
- * 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
  */
 public class ImageUtilsAndroid extends ImageUtils {
+       @Override
+       protected boolean check() {
+               // If we can get the class, it means we have access to it
+               @SuppressWarnings("unused")
+               Object test = Bitmap.class;
+               return true;
+       }
+       
        @Override
        public void saveAsImage(Image img, File target, String format)
                        throws IOException {
@@ -59,10 +72,14 @@ public class ImageUtilsAndroid extends ImageUtils {
         *             in case of IO error
         */
        static public Bitmap fromImage(Image img) throws IOException {
-               Bitmap image = BitmapFactory.decodeByteArray(img.getData(), 0,
-                               img.getData().length);
+               byte[] array = img.getData();
+               int size = array.length;
+               // TODO: check if we can use a stream, too
+               Bitmap image = BitmapFactory.decodeByteArray(array, 0, size);
                if (image == null) {
-                       throw new IOException("Failed to convert input to image");
+                       String ssize = StringUtils.formatNumber(size);
+                       throw new IOException(
+                                       "Failed to convert input to image, size was: " + ssize);
                }
 
                return image;