aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/cgeo/geocaching/Image.java20
-rw-r--r--main/src/cgeo/geocaching/network/HtmlImage.java4
-rw-r--r--main/src/cgeo/geocaching/utils/FileUtils.java28
3 files changed, 50 insertions, 2 deletions
diff --git a/main/src/cgeo/geocaching/Image.java b/main/src/cgeo/geocaching/Image.java
index b9ca3e5..50ea80e 100644
--- a/main/src/cgeo/geocaching/Image.java
+++ b/main/src/cgeo/geocaching/Image.java
@@ -1,5 +1,7 @@
package cgeo.geocaching;
+import cgeo.geocaching.utils.FileUtils;
+
import org.apache.commons.lang3.StringUtils;
import android.content.Context;
@@ -91,4 +93,22 @@ public class Image implements Parcelable {
return "???";
}
+
+ /**
+ * Check if the URL represents a file on the local file system.
+ *
+ * @return <tt>true</tt> if the URL scheme is <tt>file</tt>, <tt>false</tt> otherwise
+ */
+ public boolean isLocalFile() {
+ return FileUtils.isFileUrl(url);
+ }
+
+ /**
+ * Local file name when {@link #isLocalFile()} is <tt>true</tt>.
+ *
+ * @return the local file
+ */
+ public File localFile() {
+ return FileUtils.urlToFile(url);
+ }
}
diff --git a/main/src/cgeo/geocaching/network/HtmlImage.java b/main/src/cgeo/geocaching/network/HtmlImage.java
index 36ce9c1..9c55fe9 100644
--- a/main/src/cgeo/geocaching/network/HtmlImage.java
+++ b/main/src/cgeo/geocaching/network/HtmlImage.java
@@ -135,11 +135,11 @@ public class HtmlImage implements Html.ImageGetter {
// Explicit local file URLs are loaded from the filesystem regardless of their age. The IO part is short
// enough to make the whole operation on the computation scheduler.
- if (url.startsWith("file://")) {
+ if (FileUtils.isFileUrl(url)) {
return Observable.defer(new Func0<Observable<? extends BitmapDrawable>>() {
@Override
public Observable<? extends BitmapDrawable> call() {
- final Bitmap bitmap = loadCachedImage(new File(url.substring(7)), true).getLeft();
+ final Bitmap bitmap = loadCachedImage(FileUtils.urlToFile(url), true).getLeft();
return bitmap != null ? Observable.from(ImageUtils.scaleBitmapToFitDisplay(bitmap)) : Observable.<BitmapDrawable>empty();
}
}).subscribeOn(RxUtils.computationScheduler);
diff --git a/main/src/cgeo/geocaching/utils/FileUtils.java b/main/src/cgeo/geocaching/utils/FileUtils.java
index 4a349a8..de068fd 100644
--- a/main/src/cgeo/geocaching/utils/FileUtils.java
+++ b/main/src/cgeo/geocaching/utils/FileUtils.java
@@ -153,4 +153,32 @@ public final class FileUtils {
}
return true;
}
+
+ /**
+ * Check if the URL represents a file on the local file system.
+ *
+ * @return <tt>true</tt> if the URL scheme is <tt>file</tt>, <tt>false</tt> otherwise
+ */
+ public static boolean isFileUrl(final String url) {
+ return StringUtils.startsWith(url, "file://");
+ }
+
+ /**
+ * Build an URL from a file name.
+ *
+ * @param file a local file name
+ * @return an URL with the <tt>file</tt> scheme
+ */
+ public static String fileToUrl(final File file) {
+ return "file://" + file.getAbsolutePath();
+ }
+
+ /**
+ * Local file name when {@link #isLocalFile()} is <tt>true</tt>.
+ *
+ * @return the local file
+ */
+ public static File urlToFile(final String url) {
+ return new File(StringUtils.substring(url, 7));
+ }
}