Search in sources :

Example 11 with DecryptableUri

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;
}
Also used : InputStream(java.io.InputStream) ExifInterface(androidx.exifinterface.media.ExifInterface) DecryptableUri(org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri) FileNotFoundException(java.io.FileNotFoundException) GifDrawable(com.bumptech.glide.load.resource.gif.GifDrawable) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) ExecutionException(java.util.concurrent.ExecutionException) Pair(android.util.Pair) WorkerThread(androidx.annotation.WorkerThread)

Aggregations

DecryptableUri (org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri)11 Bitmap (android.graphics.Bitmap)3 SettableFuture (org.thoughtcrime.securesms.util.concurrent.SettableFuture)3 Context (android.content.Context)2 Canvas (android.graphics.Canvas)2 NonNull (android.support.annotation.NonNull)2 Nullable (android.support.annotation.Nullable)2 ImageView (android.widget.ImageView)2 GlideRequest (org.thoughtcrime.securesms.mms.GlideRequest)2 SuppressLint (android.annotation.SuppressLint)1 Pair (android.util.Pair)1 WorkerThread (androidx.annotation.WorkerThread)1 ExifInterface (androidx.exifinterface.media.ExifInterface)1 CenterCrop (com.bumptech.glide.load.resource.bitmap.CenterCrop)1 RoundedCorners (com.bumptech.glide.load.resource.bitmap.RoundedCorners)1 GlideDrawable (com.bumptech.glide.load.resource.drawable.GlideDrawable)1 GifDrawable (com.bumptech.glide.load.resource.gif.GifDrawable)1 GlideDrawableImageViewTarget (com.bumptech.glide.request.target.GlideDrawableImageViewTarget)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1