use of org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri in project Signal-Android by WhisperSystems.
the class MediaUtil method getDimensions.
@WorkerThread
public static Pair<Integer, Integer> getDimensions(@NonNull Context context, @Nullable String contentType, @Nullable Uri uri) {
if (uri == null || (!MediaUtil.isImageType(contentType) && !MediaUtil.isVideoType(contentType))) {
return new Pair<>(0, 0);
}
Pair<Integer, Integer> dimens = null;
if (MediaUtil.isGif(contentType)) {
try {
GifDrawable drawable = GlideApp.with(context).asGif().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).load(new DecryptableUri(uri)).submit().get();
dimens = new Pair<>(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
} catch (InterruptedException e) {
Log.w(TAG, "Was unable to complete work for GIF dimensions.", e);
} catch (ExecutionException e) {
Log.w(TAG, "Glide experienced an exception while trying to get GIF dimensions.", e);
}
} else if (MediaUtil.hasVideoThumbnail(context, uri)) {
Bitmap thumbnail = MediaUtil.getVideoThumbnail(context, uri, 1000);
if (thumbnail != null) {
dimens = new Pair<>(thumbnail.getWidth(), thumbnail.getHeight());
}
} else {
InputStream attachmentStream = null;
try {
if (MediaUtil.isJpegType(contentType)) {
attachmentStream = PartAuthority.getAttachmentStream(context, uri);
dimens = BitmapUtil.getExifDimensions(new ExifInterface(attachmentStream));
attachmentStream.close();
attachmentStream = null;
}
if (dimens == null) {
attachmentStream = PartAuthority.getAttachmentStream(context, uri);
dimens = BitmapUtil.getDimensions(attachmentStream);
}
} catch (FileNotFoundException e) {
Log.w(TAG, "Failed to find file when retrieving media dimensions.", e);
} catch (IOException e) {
Log.w(TAG, "Experienced a read error when retrieving media dimensions.", e);
} catch (BitmapDecodingException e) {
Log.w(TAG, "Bitmap decoding error when retrieving dimensions.", e);
} finally {
if (attachmentStream != null) {
try {
attachmentStream.close();
} catch (IOException e) {
Log.w(TAG, "Failed to close stream after retrieving dimensions.", e);
}
}
}
}
if (dimens == null) {
dimens = new Pair<>(0, 0);
}
Log.d(TAG, "Dimensions for [" + uri + "] are " + dimens.first + " x " + dimens.second);
return dimens;
}
Aggregations