diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java index fed838316..1f7b9d4e2 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java @@ -90,6 +90,15 @@ public static int getImageOrientation(Context ctx, String filePath) { filePath = filePath.replace("file://", ""); if (!filePath.contains("content://")) { + // A plain filesystem path pointing at an existing file (e.g. an app cache file) is not + // resolvable through the MediaStore content provider — prefixing it with + // "content://media" would produce a bogus Uri whose query always fails (throwing on + // some devices). EXIF is the only orientation source such a file has, so read it + // directly. + String existingPath = firstExistingPath(filePath, Uri.decode(filePath)); + if (existingPath != null) { + return getExifOrientation(existingPath); + } curStream = Uri.parse("content://media" + filePath); } else { curStream = Uri.parse(filePath); @@ -115,6 +124,17 @@ public static int getImageOrientation(Context ctx, String filePath) { return orientation; } + /** + * Returns the first candidate that names a file on disk, or null when none of them do. + */ + private static String firstExistingPath(String... candidates) { + for (String candidate : candidates) { + if (!TextUtils.isEmpty(candidate) && new File(candidate).exists()) { + return candidate; + } + } + return null; + } private static int getExifOrientation(String path) { if (TextUtils.isEmpty(path)) {