diff options
author | Maxime Biais <maxime.biais@gmail.com> | 2015-12-29 17:44:57 +0100 |
---|---|---|
committer | Maxime Biais <maxime.biais@gmail.com> | 2015-12-29 17:44:57 +0100 |
commit | 97672e22c0a862296c0261e96d29e2a4c55975e9 (patch) | |
tree | 2d9beec5c7fceb48f5d3b71f31831e4b02206043 /libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java | |
parent | b9b8b75746cb319a194bb551313d4b0a5c5c7c58 (diff) | |
parent | 355e34b84ad8c79589065dafb7cf19b619eb957c (diff) | |
download | gradle-perf-android-medium-97672e22c0a862296c0261e96d29e2a4c55975e9.tar.gz |
Merge commit '355e34b84ad8c79589065dafb7cf19b619eb957c' into develop
Diffstat (limited to 'libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java')
-rw-r--r-- | libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java index 75a09bab2..e0335c0af 100644 --- a/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java +++ b/libs/utils/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java @@ -20,6 +20,7 @@ import android.os.AsyncTask; import android.provider.MediaStore; import android.text.TextUtils; import android.util.Log; +import android.webkit.MimeTypeMap; import android.widget.ImageView; import org.apache.http.HttpEntity; @@ -31,6 +32,7 @@ import org.apache.http.impl.client.DefaultHttpClient; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.WeakReference; @@ -397,6 +399,60 @@ public class ImageUtils { } /** + * Given the path to an image, resize the image down to within a maximum width + * @param path the path to the original image + * @param maxWidth the maximum allowed width + * @return the path to the resized image + */ + public static String createResizedImageWithMaxWidth(Context context, String path, int maxWidth) { + File file = new File(path); + if (!file.exists()) { + return path; + } + + String mimeType = MediaUtils.getMediaFileMimeType(file); + if (mimeType.equals("image/gif")) { + // Don't rescale gifs to maintain their quality + return path; + } + + String fileName = MediaUtils.getMediaFileName(file, mimeType); + String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileName).toLowerCase(); + + int[] dimensions = getImageSize(Uri.fromFile(file), context); + int orientation = getImageOrientation(context, path); + + if (dimensions[0] <= maxWidth) { + // Image width is within limits; don't resize + return path; + } + + // Create resized image + byte[] bytes = ImageUtils.createThumbnailFromUri(context, Uri.parse(path), maxWidth, fileExtension, orientation); + + if (bytes != null) { + try { + File resizedImageFile = File.createTempFile("wp-image-", fileExtension); + FileOutputStream out = new FileOutputStream(resizedImageFile); + out.write(bytes); + out.close(); + + String tempFilePath = resizedImageFile.getPath(); + + if (!TextUtils.isEmpty(tempFilePath)) { + return tempFilePath; + } else { + AppLog.e(AppLog.T.POSTS, "Failed to create resized image"); + } + } catch (IOException e) { + AppLog.e(AppLog.T.POSTS, "Failed to create image temp file"); + } + } + + return path; + } + + /** * nbradbury - 21-Feb-2014 - similar to createThumbnail but more efficient since it doesn't * require passing the full-size image as an array of bytes[] */ |