Add 'src/be/nikiroo/utils/' from commit '46add0670fdee4bd936a13fe2448c5e20a7ffd0a'
[nikiroo-utils.git] / src / be / nikiroo / utils / android / ImageUtilsAndroid.java
index 4b3c1ea631d94689039b225925aff20e4ed57cc0..c2e269cc58291cfbcb32d2e105c7d63938c6e136 100644 (file)
@@ -1,13 +1,14 @@
 package be.nikiroo.utils.android;
 
 import android.graphics.Bitmap;
+import android.graphics.Bitmap.Config;
 import android.graphics.BitmapFactory;
 
 import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
-
-import javax.imageio.ImageIO;
+import java.io.InputStream;
+import java.util.stream.Stream;
 
 import be.nikiroo.utils.Image;
 import be.nikiroo.utils.ImageUtils;
@@ -23,11 +24,10 @@ 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;
+               Config c = Config.ALPHA_8;
                return true;
        }
-       
+
        @Override
        public void saveAsImage(Image img, File target, String format)
                        throws IOException {
@@ -45,7 +45,7 @@ public class ImageUtilsAndroid extends ImageUtils {
                        }
 
                        // Some formats are not reliable
-                       // Second change: PNG
+                       // Second chance: PNG
                        if (!ok && !format.equals("png")) {
                                ok = image.compress(Bitmap.CompressFormat.PNG, 90, fos);
                        }
@@ -72,16 +72,28 @@ public class ImageUtilsAndroid extends ImageUtils {
         *             in case of IO error
         */
        static public Bitmap fromImage(Image img) throws IOException {
-               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) {
-                       String ssize = StringUtils.formatNumber(size);
-                       throw new IOException(
-                                       "Failed to convert input to image, size was: " + ssize);
-               }
+               InputStream stream = img.newInputStream();
+               try {
+                       Bitmap image = BitmapFactory.decodeStream(stream);
+                       if (image == null) {
+                               String extra = "";
+                               if (img.getSize() <= 2048) {
+                                       try {
+                                               extra = ", content: "
+                                                               + new String(img.getData(), "UTF-8");
+                                       } catch (Exception e) {
+                                               extra = ", content unavailable";
+                                       }
+                               }
+                               String ssize = StringUtils.formatNumber(img.getSize());
+                               throw new IOException(
+                                               "Failed to convert input to image, size was: " + ssize
+                                                               + extra);
+                       }
 
-               return image;
+                       return image;
+               } finally {
+                       stream.close();
+               }
        }
 }