X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fandroid%2FImageUtilsAndroid.java;h=c2e269cc58291cfbcb32d2e105c7d63938c6e136;hb=505be508ae7d3fb48122be548b310a238cfb91eb;hp=b2359e66338d9d0d7959b6fae56fa3ea6b0c7b72;hpb=e704a41477b85b6bb76dff4181df132176aec112;p=fanfix.git diff --git a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java index b2359e6..c2e269c 100644 --- a/src/be/nikiroo/utils/android/ImageUtilsAndroid.java +++ b/src/be/nikiroo/utils/android/ImageUtilsAndroid.java @@ -1,14 +1,18 @@ 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 java.io.InputStream; +import java.util.stream.Stream; 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 @@ -17,6 +21,13 @@ import be.nikiroo.utils.ImageUtils; * @author niki */ public class ImageUtilsAndroid extends ImageUtils { + @Override + protected boolean check() { + // If we can get the class, it means we have access to it + Config c = Config.ALPHA_8; + return true; + } + @Override public void saveAsImage(Image img, File target, String format) throws IOException { @@ -34,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); } @@ -61,27 +72,28 @@ 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); - if (image == null) { - 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"; + 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); } - throw new IOException( - "Failed to convert input to image, size was: " + ssize); + return image; + } finally { + stream.close(); } - - return image; } }