Browse Source

show SVG images

AndyScherzinger 8 years ago
parent
commit
320d991c19
1 changed files with 74 additions and 41 deletions
  1. 74 41
      src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java

+ 74 - 41
src/main/java/com/owncloud/android/ui/preview/PreviewImageFragment.java

@@ -29,6 +29,7 @@ import android.graphics.Point;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LayerDrawable;
+import android.graphics.drawable.PictureDrawable;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.support.annotation.DrawableRes;
@@ -48,6 +49,8 @@ import android.widget.ProgressBar;
 import android.widget.RelativeLayout;
 import android.widget.TextView;
 
+import com.caverock.androidsvg.SVG;
+import com.caverock.androidsvg.SVGParseException;
 import com.owncloud.android.R;
 import com.owncloud.android.datamodel.OCFile;
 import com.owncloud.android.files.FileMenuFilter;
@@ -59,6 +62,8 @@ import com.owncloud.android.utils.BitmapUtils;
 import com.owncloud.android.utils.DisplayUtils;
 import com.owncloud.android.utils.MimeTypeUtil;
 
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.lang.ref.WeakReference;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -419,12 +424,14 @@ public class PreviewImageFragment extends FileFragment {
          * @param imageView Target {@link ImageView} where the bitmap will be loaded into.
          */
         public LoadBitmapTask(ImageViewCustom imageView) {
-            mImageViewRef = new WeakReference<ImageViewCustom>(imageView);
+            mImageViewRef = new WeakReference<>(imageView);
         }
 
         @Override
         protected LoadImage doInBackground(OCFile... params) {
-            Bitmap result = null;
+            Bitmap bitmapResult = null;
+            Drawable drawableResult = null;
+
             if (params.length != 1) {
                 return null;
             }
@@ -436,51 +443,72 @@ public class PreviewImageFragment extends FileFragment {
                 Point screenSize = DisplayUtils.getScreenSize(getActivity());
                 int minWidth = screenSize.x;
                 int minHeight = screenSize.y;
-                for (int i = 0; i < maxDownScale && result == null; i++) {
-                    if (isCancelled()) {
-                        return null;
-                    }
-                    try {
-                        result = BitmapUtils.decodeSampledBitmapFromFile(storagePath, minWidth,
-                                minHeight);
+                for (int i = 0; i < maxDownScale && bitmapResult == null && drawableResult == null; i++) {
 
+                    if (ocFile.getMimetype().equalsIgnoreCase("image/svg+xml")) {
                         if (isCancelled()) {
-                            return new LoadImage(result, ocFile);
+                            return null;
                         }
 
-                        if (result == null) {
-                            mErrorMessageId = R.string.preview_image_error_unknown_format;
-                            Log_OC.e(TAG, "File could not be loaded as a bitmap: " + storagePath);
-                            break;
-                        } else {
-                            if (ocFile.getMimetype().equalsIgnoreCase("image/jpeg")) {
-                                // Rotate image, obeying exif tag.
-                                result = BitmapUtils.rotateImage(result, storagePath);
+                        try {
+                            SVG svg = SVG.getFromInputStream(new FileInputStream(storagePath));
+                            drawableResult = new PictureDrawable(svg.renderToPicture());
+
+                            if (isCancelled()) {
+                                return new LoadImage(null, drawableResult, ocFile);
                             }
+                        } catch (FileNotFoundException e) {
+                            mErrorMessageId = R.string.common_error_unknown;
+                            Log_OC.e(TAG, "File not found trying to load " + getFile().getStoragePath(), e);
+                        } catch (SVGParseException e) {
+                            mErrorMessageId = R.string.common_error_unknown;
+                            Log_OC.e(TAG, "Couldn't parse SVG " + getFile().getStoragePath(), e);
                         }
-
-                    } catch (OutOfMemoryError e) {
-                        mErrorMessageId = R.string.common_error_out_memory;
-                        if (i < maxDownScale - 1) {
-                            Log_OC.w(TAG, "Out of memory rendering file " + storagePath +
-                                    " ; scaling down");
-                            minWidth = minWidth / 2;
-                            minHeight = minHeight / 2;
-
-                        } else {
-                            Log_OC.w(TAG, "Out of memory rendering file " + storagePath +
-                                    " ; failing");
+                    } else {
+                        if (isCancelled()) {
+                            return null;
                         }
-                        if (result != null) {
-                            result.recycle();
+
+                        try {
+                            bitmapResult = BitmapUtils.decodeSampledBitmapFromFile(storagePath, minWidth,
+                                    minHeight);
+
+                            if (isCancelled()) {
+                                return new LoadImage(bitmapResult, null, ocFile);
+                            }
+
+                            if (bitmapResult == null) {
+                                mErrorMessageId = R.string.preview_image_error_unknown_format;
+                                Log_OC.e(TAG, "File could not be loaded as a bitmap: " + storagePath);
+                                break;
+                            } else {
+                                if (ocFile.getMimetype().equalsIgnoreCase("image/jpeg")) {
+                                    // Rotate image, obeying exif tag.
+                                    bitmapResult = BitmapUtils.rotateImage(bitmapResult, storagePath);
+                                }
+                            }
+
+                        } catch (OutOfMemoryError e) {
+                            mErrorMessageId = R.string.common_error_out_memory;
+                            if (i < maxDownScale - 1) {
+                                Log_OC.w(TAG, "Out of memory rendering file " + storagePath + " ; scaling down");
+                                minWidth = minWidth / 2;
+                                minHeight = minHeight / 2;
+
+                            } else {
+                                Log_OC.w(TAG, "Out of memory rendering file " + storagePath + " ; failing");
+                            }
+                            if (bitmapResult != null) {
+                                bitmapResult.recycle();
+                            }
+                            bitmapResult = null;
                         }
-                        result = null;
                     }
                 }
 
             } catch (NoSuchFieldError e) {
                 mErrorMessageId = R.string.common_error_unknown;
-                Log_OC.e(TAG, "Error from access to unexisting field despite protection; file "
+                Log_OC.e(TAG, "Error from access to non-existing field despite protection; file "
                         + storagePath, e);
 
             } catch (Throwable t) {
@@ -489,7 +517,7 @@ public class PreviewImageFragment extends FileFragment {
 
             }
 
-            return new LoadImage(result, ocFile);
+            return new LoadImage(bitmapResult, drawableResult, ocFile);
         }
 
         @Override
@@ -501,7 +529,7 @@ public class PreviewImageFragment extends FileFragment {
 
         @Override
         protected void onPostExecute(LoadImage result) {
-            if (result.bitmap != null) {
+            if (result.bitmap != null || result.drawable != null) {
                 showLoadedImage(result);
             } else {
                 showErrorMessage();
@@ -517,9 +545,10 @@ public class PreviewImageFragment extends FileFragment {
             final ImageViewCustom imageView = mImageViewRef.get();
             Bitmap bitmap = result.bitmap;
             if (imageView != null) {
-                Log_OC.d(TAG, "Showing image with resolution " + bitmap.getWidth() + "x" +
-                        bitmap.getHeight());
-
+                if(bitmap != null) {
+                    Log_OC.d(TAG, "Showing image with resolution " + bitmap.getWidth() + "x" +
+                            bitmap.getHeight());
+                }
 
                 if (result.ocFile.getMimetype().equalsIgnoreCase("image/png")) {
                     if (getResources() != null) {
@@ -539,7 +568,9 @@ public class PreviewImageFragment extends FileFragment {
                     }
                 }
 
-                if (result.ocFile.getMimetype().equalsIgnoreCase("image/gif")) {
+                if (result.ocFile.getMimetype().equalsIgnoreCase("image/svg+xml")) {
+                    imageView.setImageDrawable(result.drawable);
+                } else if (result.ocFile.getMimetype().equalsIgnoreCase("image/gif")) {
                     imageView.setGIFImageFromStoragePath(result.ocFile.getStoragePath());
                 } else if (!result.ocFile.getMimetype().equalsIgnoreCase("image/png")) {
                     imageView.setImageBitmap(bitmap);
@@ -640,10 +671,12 @@ public class PreviewImageFragment extends FileFragment {
 
     private class LoadImage {
         private Bitmap bitmap;
+        private Drawable drawable;
         private OCFile ocFile;
 
-        public LoadImage(Bitmap bitmap, OCFile ocFile) {
+        public LoadImage(Bitmap bitmap, Drawable drawable, OCFile ocFile) {
             this.bitmap = bitmap;
+            this.drawable = drawable;
             this.ocFile = ocFile;
         }