diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2013-09-15 15:25:04 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2013-09-15 15:25:04 +0200 |
| commit | 309f5d134c9b5e23ac3423f9dbb06515aee695d0 (patch) | |
| tree | c4deb4928f652e3fae2ccf8603e672457d126112 /main/src/cgeo/geocaching/network/HtmlImage.java | |
| parent | c12ba5eb9ed6606defb4e3839d8ba009f6d996ff (diff) | |
| parent | 87cd8166f0598314c7744ec11dc29278ad8ad372 (diff) | |
| download | cgeo-309f5d134c9b5e23ac3423f9dbb06515aee695d0.zip cgeo-309f5d134c9b5e23ac3423f9dbb06515aee695d0.tar.gz cgeo-309f5d134c9b5e23ac3423f9dbb06515aee695d0.tar.bz2 | |
Merge branch 'release' into upstream
Diffstat (limited to 'main/src/cgeo/geocaching/network/HtmlImage.java')
| -rw-r--r-- | main/src/cgeo/geocaching/network/HtmlImage.java | 54 |
1 files changed, 38 insertions, 16 deletions
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java index 953d762..ed8eeed 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); } } } |
