From 8ea42ba3c77ac2b1bc8acec0ecb3a1da91fe8fef Mon Sep 17 00:00:00 2001 From: EggMeister Date: Sat, 13 Feb 2016 20:45:19 +0100 Subject: [PATCH] Added support for base64-encoded images Added support for opening images directly as a base64-encoded string. This already worked in iOS, but threw an 'invalid URL'-error on Android. --- src/android/PhotoActivity.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/android/PhotoActivity.java b/src/android/PhotoActivity.java index 40da16d..ff01cc6 100644 --- a/src/android/PhotoActivity.java +++ b/src/android/PhotoActivity.java @@ -3,10 +3,12 @@ import uk.co.senab.photoview.PhotoViewAttacher; import android.app.Activity; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.os.Environment; +import android.util.Base64; import android.view.View; import android.widget.ImageButton; import android.widget.ImageView; @@ -137,11 +139,18 @@ public void onError() { finish(); } }); - } else { - photo.setImageURI(Uri.parse(imageUrl)); - - hideLoadingAndUpdate(); - } + } else if ( imageUrl.startsWith("data:image")){ + String base64String = imageUrl.substring(imageUrl.indexOf(",")+1); + byte[] decodedString = Base64.decode(base64String, Base64.DEFAULT); + Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); + photo.setImageBitmap(decodedByte); + + hideLoadingAndUpdate(); + } else { + photo.setImageURI(Uri.parse(imageUrl)); + + hideLoadingAndUpdate(); + } } /**