Skip to content

Commit a0920e0

Browse files
committed
Merge commit '355e34b84ad8c79589065dafb7cf19b619eb957c' into develop
2 parents 2a47170 + 355e34b commit a0920e0

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

WordPressUtils/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ android {
3232
buildToolsVersion "23.0.2"
3333

3434
defaultConfig {
35-
versionName "1.5.0"
35+
versionName "1.7.0"
3636
minSdkVersion 14
3737
targetSdkVersion 23
3838
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.wordpress.android.util;
2+
3+
import java.io.IOException;
4+
import java.net.HttpURLConnection;
5+
import java.net.URL;
6+
import java.util.Map;
7+
8+
public class HTTPUtils {
9+
public static final int REQUEST_TIMEOUT_MS = 30000;
10+
11+
/**
12+
* Builds an HttpURLConnection from a URL and header map. Will force HTTPS usage if given an Authorization header.
13+
* @throws IOException
14+
*/
15+
public static HttpURLConnection setupUrlConnection(String url, Map<String, String> headers) throws IOException {
16+
// Force HTTPS usage if an authorization header was specified
17+
if (headers.keySet().contains("Authorization")) {
18+
url = UrlUtils.makeHttps(url);
19+
}
20+
21+
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
22+
conn.setReadTimeout(REQUEST_TIMEOUT_MS);
23+
conn.setConnectTimeout(REQUEST_TIMEOUT_MS);
24+
25+
for (Map.Entry<String, String> entry : headers.entrySet()) {
26+
conn.setRequestProperty(entry.getKey(), entry.getValue());
27+
}
28+
29+
return conn;
30+
}
31+
}

WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import android.provider.MediaStore;
2121
import android.text.TextUtils;
2222
import android.util.Log;
23+
import android.webkit.MimeTypeMap;
2324
import android.widget.ImageView;
2425

2526
import org.apache.http.HttpEntity;
@@ -31,6 +32,7 @@
3132
import java.io.ByteArrayOutputStream;
3233
import java.io.File;
3334
import java.io.FileInputStream;
35+
import java.io.FileOutputStream;
3436
import java.io.IOException;
3537
import java.io.InputStream;
3638
import java.lang.ref.WeakReference;
@@ -396,6 +398,60 @@ public static Bitmap getScaledBitmapAtLongestSide(Bitmap bitmap, int targetSize)
396398
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
397399
}
398400

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+
399455
/**
400456
* nbradbury - 21-Feb-2014 - similar to createThumbnail but more efficient since it doesn't
401457
* require passing the full-size image as an array of bytes[]

0 commit comments

Comments
 (0)