|
20 | 20 | import android.provider.MediaStore; |
21 | 21 | import android.text.TextUtils; |
22 | 22 | import android.util.Log; |
| 23 | +import android.webkit.MimeTypeMap; |
23 | 24 | import android.widget.ImageView; |
24 | 25 |
|
25 | 26 | import org.apache.http.HttpEntity; |
|
31 | 32 | import java.io.ByteArrayOutputStream; |
32 | 33 | import java.io.File; |
33 | 34 | import java.io.FileInputStream; |
| 35 | +import java.io.FileOutputStream; |
34 | 36 | import java.io.IOException; |
35 | 37 | import java.io.InputStream; |
36 | 38 | import java.lang.ref.WeakReference; |
@@ -396,6 +398,60 @@ public static Bitmap getScaledBitmapAtLongestSide(Bitmap bitmap, int targetSize) |
396 | 398 | return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true); |
397 | 399 | } |
398 | 400 |
|
| 401 | + /** |
| 402 | + * Given the path to an image, resize the image down to within a maximum width |
| 403 | + * @param path the path to the original image |
| 404 | + * @param maxWidth the maximum allowed width |
| 405 | + * @return the path to the resized image |
| 406 | + */ |
| 407 | + public static String createResizedImageWithMaxWidth(Context context, String path, int maxWidth) { |
| 408 | + File file = new File(path); |
| 409 | + if (!file.exists()) { |
| 410 | + return path; |
| 411 | + } |
| 412 | + |
| 413 | + String mimeType = MediaUtils.getMediaFileMimeType(file); |
| 414 | + if (mimeType.equals("image/gif")) { |
| 415 | + // Don't rescale gifs to maintain their quality |
| 416 | + return path; |
| 417 | + } |
| 418 | + |
| 419 | + String fileName = MediaUtils.getMediaFileName(file, mimeType); |
| 420 | + String fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileName).toLowerCase(); |
| 421 | + |
| 422 | + int[] dimensions = getImageSize(Uri.fromFile(file), context); |
| 423 | + int orientation = getImageOrientation(context, path); |
| 424 | + |
| 425 | + if (dimensions[0] <= maxWidth) { |
| 426 | + // Image width is within limits; don't resize |
| 427 | + return path; |
| 428 | + } |
| 429 | + |
| 430 | + // Create resized image |
| 431 | + byte[] bytes = ImageUtils.createThumbnailFromUri(context, Uri.parse(path), maxWidth, fileExtension, orientation); |
| 432 | + |
| 433 | + if (bytes != null) { |
| 434 | + try { |
| 435 | + File resizedImageFile = File.createTempFile("wp-image-", fileExtension); |
| 436 | + FileOutputStream out = new FileOutputStream(resizedImageFile); |
| 437 | + out.write(bytes); |
| 438 | + out.close(); |
| 439 | + |
| 440 | + String tempFilePath = resizedImageFile.getPath(); |
| 441 | + |
| 442 | + if (!TextUtils.isEmpty(tempFilePath)) { |
| 443 | + return tempFilePath; |
| 444 | + } else { |
| 445 | + AppLog.e(AppLog.T.POSTS, "Failed to create resized image"); |
| 446 | + } |
| 447 | + } catch (IOException e) { |
| 448 | + AppLog.e(AppLog.T.POSTS, "Failed to create image temp file"); |
| 449 | + } |
| 450 | + } |
| 451 | + |
| 452 | + return path; |
| 453 | + } |
| 454 | + |
399 | 455 | /** |
400 | 456 | * nbradbury - 21-Feb-2014 - similar to createThumbnail but more efficient since it doesn't |
401 | 457 | * require passing the full-size image as an array of bytes[] |
|
0 commit comments