[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge commit '355e34b84ad8c79589065dafb7cf19b619eb957c' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
maxme committed Dec 29, 2015
2 parents 74fdb6d + 355e34b commit 135651f
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion WordPressUtils/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ android {
buildToolsVersion "23.0.2"

defaultConfig {
versionName "1.5.0"
versionName "1.7.0"
minSdkVersion 14
targetSdkVersion 23
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.wordpress.android.util;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;

public class HTTPUtils {
public static final int REQUEST_TIMEOUT_MS = 30000;

/**
* Builds an HttpURLConnection from a URL and header map. Will force HTTPS usage if given an Authorization header.
* @throws IOException
*/
public static HttpURLConnection setupUrlConnection(String url, Map<String, String> headers) throws IOException {
// Force HTTPS usage if an authorization header was specified
if (headers.keySet().contains("Authorization")) {
url = UrlUtils.makeHttps(url);
}

HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setReadTimeout(REQUEST_TIMEOUT_MS);
conn.setConnectTimeout(REQUEST_TIMEOUT_MS);

for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}

return conn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand All @@ -31,6 +32,7 @@
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;
Expand Down Expand Up @@ -396,6 +398,60 @@ public static Bitmap getScaledBitmapAtLongestSide(Bitmap bitmap, int targetSize)
return Bitmap.createScaledBitmap(bitmap, targetWidth, targetHeight, true);
}

/**
* 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[]
Expand Down

0 comments on commit 135651f

Please sign in to comment.