Skip to content

Commit

Permalink
make ImageList.MD5() thread-safe and ignore ExifInterface errors when…
Browse files Browse the repository at this point in the history
… loading cached images
  • Loading branch information
c99koder committed Feb 26, 2019
1 parent 07909a0 commit a4bd6dd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/com/irccloud/android/NetworkConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2293,6 +2293,7 @@ public void parse(IRCCloudJSONObject object) throws JSONException {
String avatar_url = null;
if(PreferenceManager.getDefaultSharedPreferences(IRCCloudApplication.getInstance().getApplicationContext()).getBoolean("avatar-images", false)) {
Event e = new Event();
e.cid = server.getCid();
e.from = server.getNick();
e.type = "buffer_msg";
e.hostmask = server.getUsermask();
Expand Down
77 changes: 39 additions & 38 deletions src/com/irccloud/android/data/collection/ImageList.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
import static com.irccloud.android.NetworkConnection.printStackTraceToCrashlytics;

public class ImageList {
private static java.security.MessageDigest md;
private static ImageList instance = null;
private HashMap<String, Bitmap> images;
private HashMap<String, GifDrawable> GIFs;
Expand Down Expand Up @@ -252,43 +251,46 @@ public Bitmap getImage(URL url, int width) throws OutOfMemoryError, FileNotFound
else
bitmap = BitmapFactory.decodeFile(cacheFile(url).getAbsolutePath());

ExifInterface exif = new ExifInterface(cacheFile(url).getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
if(orientation > 1) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
}
try {
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
Log.e("IRCCloud", "Out of memory rotating the photo");
try {
ExifInterface exif = new ExifInterface(cacheFile(url).getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
if (orientation > 1) {
Matrix matrix = new Matrix();
switch (orientation) {
case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
matrix.setScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
matrix.setRotate(180);
break;
case ExifInterface.ORIENTATION_FLIP_VERTICAL:
matrix.setRotate(180);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_TRANSPOSE:
matrix.setRotate(90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_90:
matrix.setRotate(90);
break;
case ExifInterface.ORIENTATION_TRANSVERSE:
matrix.setRotate(-90);
matrix.postScale(-1, 1);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
matrix.setRotate(-90);
break;
}
try {
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
} catch (OutOfMemoryError e) {
Log.e("IRCCloud", "Out of memory rotating the photo");
}
}
} catch (Exception e) {
e.printStackTrace();
}

if (bitmap != null)
images.put(MD5(url.toString()), bitmap);
}
Expand Down Expand Up @@ -535,8 +537,7 @@ public File cacheFile(URL url) {

public static String MD5(String md5) {
try {
if(md == null)
md = java.security.MessageDigest.getInstance("MD5");
java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
byte[] array = md.digest(md5.getBytes(Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
for (byte anArray : array) {
Expand Down

0 comments on commit a4bd6dd

Please sign in to comment.