public void loadImage(String imageUrl, GlideImageView giv) { if (mCtx == null) return; if (Build.VERSION.SDK_INT >= 17 && (mCtx instanceof Activity) && ((Activity) mCtx).isDestroyed()) return; if (!isAdded() || isDetached()) return; ImageReadyInfo imageReadyInfo = ImageContainer.getImageInfo(imageUrl); if (imageReadyInfo != null && imageReadyInfo.isReady()) { giv.getLayoutParams().width = imageReadyInfo.getWidth(); giv.getLayoutParams().height = imageReadyInfo.getHeight(); if (imageReadyInfo.getWidth() > GlideImageView.MIN_SCALE_WIDTH || imageReadyInfo.isGif()) { giv.setImageReadyInfo(imageReadyInfo); giv.setClickToViewBigImage(); } if (imageReadyInfo.isGif()) { Glide.with(mCtx) .load(GlideHelper.getGlideUrl(imageUrl)) .asBitmap() .diskCacheStrategy(DiskCacheStrategy.ALL) .transform(new GifTransformation(mCtx)) .into( new GlideBitmapTarget(giv, imageReadyInfo.getWidth(), imageReadyInfo.getHeight())); } else { Glide.with(mCtx) .load(GlideHelper.getGlideUrl(imageUrl)) .asBitmap() .cacheDecoder(new FileToStreamDecoder<>(new ThreadImageDecoder(mMaxImageDecodeWidth))) .imageDecoder(new ThreadImageDecoder(mMaxImageDecodeWidth)) .diskCacheStrategy(DiskCacheStrategy.ALL) .into( new GlideBitmapTarget(giv, imageReadyInfo.getWidth(), imageReadyInfo.getHeight())); } } else { giv.setImageResource(R.drawable.tapatalk_image_broken); } }
@Override public void onRun() throws Throwable { try { FutureTarget<File> future = Glide.with(mCtx) .load(GlideHelper.getGlideUrl(mUrl)) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); File cacheFile = future.get(); Glide.clear(future); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // Returns null, sizes are in the options variable BitmapFactory.decodeFile(cacheFile.getPath(), options); int width = options.outWidth; int height = options.outHeight; String mime = Utils.nullToText(options.outMimeType); int orientation = 0; if (mime.toLowerCase().contains("jpeg") || mime.toLowerCase().contains("jpg") || mime.toLowerCase().contains("png")) { try { ExifInterface exif = new ExifInterface(cacheFile.getPath()); orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0); } catch (Exception e) { Logger.e(e); } } if (orientation == ExifInterface.ORIENTATION_ROTATE_90 || orientation == ExifInterface.ORIENTATION_ROTATE_270) { width = options.outHeight; height = options.outWidth; } // calculate display size for image // leave 12dp on both left and right side, this should match layout setup int maxViewWidth = ThreadDetailFragment.MAX_VIEW_WIDTH - dpToPx(12 * 2); // if image width < half maxViewWidth, scale it up for better view int maxScaleWidth = Math.round(maxViewWidth * 0.5f); double scaleRate = getScaleRate(width); int scaledWidth = Math.round((int) (width * scaleRate)); int scaledHeight = Math.round((int) (height * scaleRate)); int displayWidth; int displayHeight; if (scaledWidth >= maxScaleWidth || (mime.toLowerCase().contains("gif") && scaledWidth >= maxScaleWidth / 2)) { displayWidth = maxViewWidth; displayHeight = Math.round(maxViewWidth * 1.0f * height / width); } else { displayWidth = scaledWidth; displayHeight = scaledHeight; } ImageReadyInfo imageReadyInfo = new ImageReadyInfo(cacheFile.getPath(), displayWidth, displayHeight, mime); if (orientation > 0) imageReadyInfo.setOrientation(orientation); ImageContainer.markImageReady(mUrl, imageReadyInfo); EventBus.getDefault().post(new GlideImageEvent(mUrl, mImageView, Constants.STATUS_SUCCESS)); } catch (Exception e) { Logger.e(e); EventBus.getDefault().post(new GlideImageEvent(mUrl, mImageView, Constants.STATUS_FAIL)); } }