|
29 | 29 | import java.util.TimeZone; |
30 | 30 |
|
31 | 31 | public class MediaUtils { |
| 32 | + private static final int DEFAULT_MAX_IMAGE_WIDTH = 1024; |
| 33 | + |
32 | 34 | public static boolean isValidImage(String url) { |
33 | 35 | if (url == null) { |
34 | 36 | return false; |
@@ -114,29 +116,52 @@ public static Uri getLastRecordedVideoUri(Activity activity) { |
114 | 116 | return Uri.parse(contentUri.toString() + "/" + cursor.getLong(0)); |
115 | 117 | } |
116 | 118 |
|
117 | | - // Calculate the minimun width between the blog setting and picture real width |
118 | | - public static int getMinimumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) { |
119 | | - int imageWidthBlogSetting = Integer.MAX_VALUE; |
120 | | - |
121 | | - if (!imageWidthBlogSettingString.equals("Original Size")) { |
122 | | - try { |
123 | | - imageWidthBlogSetting = Integer.valueOf(imageWidthBlogSettingString); |
124 | | - } catch (NumberFormatException e) { |
125 | | - AppLog.e(T.POSTS, e); |
126 | | - } |
| 119 | + /** |
| 120 | + * Get image width setting from the image width site setting string. This string can be an int, in this case it's |
| 121 | + * the maximum image width defined by the site. |
| 122 | + * Examples: |
| 123 | + * "1000" will return 1000 |
| 124 | + * "Original Size" will return Integer.MAX_VALUE |
| 125 | + * "Largeur originale" will return Integer.MAX_VALUE |
| 126 | + * null will return Integer.MAX_VALUE |
| 127 | + * @param imageWidthSiteSettingString Image width site setting string |
| 128 | + * @return Integer.MAX_VALUE if image width is not defined or invalid, maximum image width in other cases. |
| 129 | + */ |
| 130 | + public static int getImageWidthSettingFromString(String imageWidthSiteSettingString) { |
| 131 | + if (imageWidthSiteSettingString == null) { |
| 132 | + return Integer.MAX_VALUE; |
| 133 | + } |
| 134 | + try { |
| 135 | + return Integer.valueOf(imageWidthSiteSettingString); |
| 136 | + } catch (NumberFormatException e) { |
| 137 | + return Integer.MAX_VALUE; |
127 | 138 | } |
| 139 | + } |
128 | 140 |
|
129 | | - int[] dimensions = ImageUtils.getImageSize(curStream, context); |
130 | | - int imageWidthPictureSetting = dimensions[0] == 0 ? Integer.MAX_VALUE : dimensions[0]; |
| 141 | + /** |
| 142 | + * Calculate and return the maximum allowed image width by comparing the width of the image at its full size with |
| 143 | + * the maximum upload width set in the blog settings |
| 144 | + * @param imageWidth the image's natural (full) width |
| 145 | + * @param imageWidthSiteSettingString the maximum upload width set in the site settings |
| 146 | + * @return maximum allowed image width |
| 147 | + */ |
| 148 | + public static int getMaximumImageWidth(int imageWidth, String imageWidthSiteSettingString) { |
| 149 | + int imageWidthBlogSetting = getImageWidthSettingFromString(imageWidthSiteSettingString); |
| 150 | + int imageWidthPictureSetting = imageWidth == 0 ? Integer.MAX_VALUE : imageWidth; |
131 | 151 |
|
132 | 152 | if (Math.min(imageWidthPictureSetting, imageWidthBlogSetting) == Integer.MAX_VALUE) { |
133 | | - // Default value in case of errors reading the picture size and the blog settings is set to Original size |
134 | | - return 1024; |
| 153 | + // Default value in case of errors reading the picture size or the blog settings is set to Original size |
| 154 | + return DEFAULT_MAX_IMAGE_WIDTH; |
135 | 155 | } else { |
136 | 156 | return Math.min(imageWidthPictureSetting, imageWidthBlogSetting); |
137 | 157 | } |
138 | 158 | } |
139 | 159 |
|
| 160 | + public static int getMaximumImageWidth(Context context, Uri curStream, String imageWidthBlogSettingString) { |
| 161 | + int[] dimensions = ImageUtils.getImageSize(curStream, context); |
| 162 | + return getMaximumImageWidth(dimensions[0], imageWidthBlogSettingString); |
| 163 | + } |
| 164 | + |
140 | 165 | public static boolean isInMediaStore(Uri mediaUri) { |
141 | 166 | // Check if the image is externally hosted (Picasa/Google Photos for example) |
142 | 167 | if (mediaUri != null && mediaUri.toString().startsWith("content://media/")) { |
|
0 commit comments