aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils/ImageUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/utils/ImageUtils.java')
-rw-r--r--main/src/cgeo/geocaching/utils/ImageUtils.java54
1 files changed, 48 insertions, 6 deletions
diff --git a/main/src/cgeo/geocaching/utils/ImageUtils.java b/main/src/cgeo/geocaching/utils/ImageUtils.java
index 0d8bec8..e226703 100644
--- a/main/src/cgeo/geocaching/utils/ImageUtils.java
+++ b/main/src/cgeo/geocaching/utils/ImageUtils.java
@@ -3,6 +3,7 @@ package cgeo.geocaching.utils;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.compatibility.Compatibility;
+import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
@@ -15,11 +16,16 @@ import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.media.ExifInterface;
import android.net.Uri;
+import android.os.Environment;
+import android.util.Base64;
+import android.util.Base64InputStream;
import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
@@ -183,11 +189,13 @@ public final class ImageUtils {
sampleOptions.inSampleSize = sampleSize;
}
final Bitmap decodedImage = BitmapFactory.decodeFile(filePath, sampleOptions);
- for (int i = 0; i < ORIENTATIONS.length; i++) {
- if (orientation == ORIENTATIONS[i]) {
- final Matrix matrix = new Matrix();
- matrix.postRotate(ROTATION[i]);
- return Bitmap.createBitmap(decodedImage, 0, 0, decodedImage.getWidth(), decodedImage.getHeight(), matrix, true);
+ if (decodedImage != null) {
+ for (int i = 0; i < ORIENTATIONS.length; i++) {
+ if (orientation == ORIENTATIONS[i]) {
+ final Matrix matrix = new Matrix();
+ matrix.postRotate(ROTATION[i]);
+ return Bitmap.createBitmap(decodedImage, 0, 0, decodedImage.getWidth(), decodedImage.getHeight(), matrix, true);
+ }
}
}
return decodedImage;
@@ -202,8 +210,8 @@ public final class ImageUtils {
public static File getOutputImageFile() {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
+ final File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "cgeo");
- File mediaStorageDir = new File(Compatibility.getExternalPictureDir(), "cgeo");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
@@ -244,4 +252,38 @@ public final class ImageUtils {
}
return false;
}
+
+ /**
+ * Decode a base64-encoded string and save the result into a file.
+ *
+ * @param inString the encoded string
+ * @param outFile the file to save the decoded result into
+ */
+ public static void decodeBase64ToFile(final String inString, final File outFile) {
+ FileOutputStream out = null;
+ try {
+ out = new FileOutputStream(outFile);
+ decodeBase64ToStream(inString, out);
+ } catch (final IOException e) {
+ Log.e("HtmlImage.decodeBase64ToFile: cannot write file for decoded inline image", e);
+ } finally {
+ IOUtils.closeQuietly(out);
+ }
+ }
+
+ /**
+ * Decode a base64-encoded string and save the result into a stream.
+ *
+ * @param inString the encoded string
+ * @param outFile the file to save the decoded result into
+ */
+ public static void decodeBase64ToStream(final String inString, final OutputStream out) throws IOException {
+ Base64InputStream in = null;
+ try {
+ in = new Base64InputStream(new ByteArrayInputStream(inString.getBytes(TextUtils.CHARSET_ASCII)), Base64.DEFAULT);
+ IOUtils.copy(in, out);
+ } finally {
+ IOUtils.closeQuietly(in);
+ }
+ }
}