diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2013-09-15 15:24:27 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2013-09-15 15:24:27 +0200 |
| commit | 87cd8166f0598314c7744ec11dc29278ad8ad372 (patch) | |
| tree | ad327968551bf62cfcac4b97452450c1f42a6f1a /main/src | |
| parent | ff7d0a1feb9f1507e90a64b66878507cd6d0e0f0 (diff) | |
| download | cgeo-87cd8166f0598314c7744ec11dc29278ad8ad372.zip cgeo-87cd8166f0598314c7744ec11dc29278ad8ad372.tar.gz cgeo-87cd8166f0598314c7744ec11dc29278ad8ad372.tar.bz2 | |
fix #3262: c:geo does not handle inline images
Diffstat (limited to 'main/src')
| -rw-r--r-- | main/src/cgeo/geocaching/files/LocalStorage.java | 9 | ||||
| -rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 54 |
2 files changed, 46 insertions, 17 deletions
diff --git a/main/src/cgeo/geocaching/files/LocalStorage.java b/main/src/cgeo/geocaching/files/LocalStorage.java index fc82409..de1908d 100644 --- a/main/src/cgeo/geocaching/files/LocalStorage.java +++ b/main/src/cgeo/geocaching/files/LocalStorage.java @@ -99,7 +99,14 @@ public final class LocalStorage { * @return the file extension, including the leading dot, or the empty string if none could be determined */ static String getExtension(final String url) { - final String urlExt = StringUtils.substringAfterLast(url, "."); + String urlExt; + if (url.startsWith("data:")) { + // "data:image/png;base64,i53…" -> ".png" + urlExt = StringUtils.substringAfter(StringUtils.substringBefore(url, ";"), "/"); + } else { + // "http://example.com/foo/bar.png" -> ".png" + urlExt = StringUtils.substringAfterLast(url, "."); + } return urlExt.length() >= 1 && urlExt.length() <= 4 ? "." + urlExt : ""; } diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 797e67d..079e254 100644 --- a/main/src/cgeo/geocaching/network/HtmlImage.java +++ b/main/src/cgeo/geocaching/network/HtmlImage.java @@ -12,7 +12,6 @@ import cgeo.geocaching.utils.ImageUtils; import cgeo.geocaching.utils.Log; import ch.boye.httpclientandroidlib.HttpResponse; - import org.apache.commons.lang3.StringUtils; import android.content.res.Resources; @@ -22,11 +21,15 @@ import android.graphics.Point; import android.graphics.drawable.BitmapDrawable; import android.net.Uri; import android.text.Html; +import android.util.Base64; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; import java.util.Date; public class HtmlImage implements Html.ImageGetter { @@ -91,24 +94,43 @@ public class HtmlImage implements Html.ImageGetter { // Download image and save it to the cache if (imagePre == null) { - final String absoluteURL = makeAbsoluteURL(url); - - if (absoluteURL != null) { - try { - final File file = LocalStorage.getStorageFile(pseudoGeocode, url, true, true); - final HttpResponse httpResponse = Network.getRequest(absoluteURL, null, file); - if (httpResponse != null) { - final int statusCode = httpResponse.getStatusLine().getStatusCode(); - if (statusCode == 200) { - LocalStorage.saveEntityToFile(httpResponse, file); - } else if (statusCode == 304) { - if (!file.setLastModified(System.currentTimeMillis())) { - makeFreshCopy(file); + final File file = LocalStorage.getStorageFile(pseudoGeocode, url, true, true); + if (url.startsWith("data:image/")) { + if (url.contains(";base64,")) { + byte[] decoded = Base64.decode(StringUtils.substringAfter(url, ";base64,"), Base64.DEFAULT); + OutputStream out = null; + try { + out = new FileOutputStream(file); + out.write(decoded); + } catch (final IOException e) { + Log.e("HtmlImage.getDrawable: cannot write file for decoded inline image", e); + return null; + } finally { + IOUtils.closeQuietly(out); + } + } else { + Log.e("HtmlImage.getDrawable: unable to decode non-base64 inline image"); + return null; + } + } else { + final String absoluteURL = makeAbsoluteURL(url); + + if (absoluteURL != null) { + try { + final HttpResponse httpResponse = Network.getRequest(absoluteURL, null, file); + if (httpResponse != null) { + final int statusCode = httpResponse.getStatusLine().getStatusCode(); + if (statusCode == 200) { + LocalStorage.saveEntityToFile(httpResponse, file); + } else if (statusCode == 304) { + if (!file.setLastModified(System.currentTimeMillis())) { + makeFreshCopy(file); + } } } + } catch (Exception e) { + Log.e("HtmlImage.getDrawable (downloading from web)", e); } - } catch (Exception e) { - Log.e("HtmlImage.getDrawable (downloading from web)", e); } } } |
