private Bitmap tryLoadBitmap() throws TaskCancelledException { File imageFile = getImageFileInDiscCache(); Bitmap bitmap = null; try { String cacheFileUri = Scheme.FILE.wrap(imageFile.getAbsolutePath()); if (imageFile.exists()) { log(LOG_LOAD_IMAGE_FROM_DISC_CACHE); loadedFrom = LoadedFrom.DISC_CACHE; checkTaskNotActual(); bitmap = decodeImage(cacheFileUri); } if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) { log(LOG_LOAD_IMAGE_FROM_NETWORK); loadedFrom = LoadedFrom.NETWORK; String imageUriForDecoding = options.isCacheOnDisc() && tryCacheImageOnDisc(imageFile) ? cacheFileUri : uri; checkTaskNotActual(); bitmap = decodeImage(imageUriForDecoding); if (bitmap == null || bitmap.getWidth() <= 0 || bitmap.getHeight() <= 0) { fireFailEvent(FailType.DECODING_ERROR, null); } } } catch (IllegalStateException e) { fireFailEvent(FailType.NETWORK_DENIED, null); } catch (TaskCancelledException e) { throw e; } catch (IOException e) { L.e(e); fireFailEvent(FailType.IO_ERROR, e); if (imageFile.exists()) { imageFile.delete(); } } catch (OutOfMemoryError e) { L.e(e); fireFailEvent(FailType.OUT_OF_MEMORY, e); } catch (Throwable e) { L.e(e); fireFailEvent(FailType.UNKNOWN, e); } return bitmap; }
/** Decodes image file into Bitmap, resize it and save it back */ private void resizeAndSaveImage(File targetFile, int maxWidth, int maxHeight) throws IOException { // Decode image file, compress and re-save it ImageSize targetImageSize = new ImageSize(maxWidth, maxHeight); DisplayImageOptions specialOptions = new DisplayImageOptions.Builder() .cloneFrom(options) .imageScaleType(ImageScaleType.IN_SAMPLE_INT) .build(); ImageDecodingInfo decodingInfo = new ImageDecodingInfo( memoryCacheKey, Scheme.FILE.wrap(targetFile.getAbsolutePath()), targetImageSize, ViewScaleType.FIT_INSIDE, getDownloader(), specialOptions); Bitmap bmp = decoder.decode(decodingInfo); if (bmp == null) return; if (configuration.processorForDiscCache != null) { log(LOG_PROCESS_IMAGE_BEFORE_CACHE_ON_DISC); bmp = configuration.processorForDiscCache.process(bmp); if (bmp == null) { L.e(ERROR_PROCESSOR_FOR_DISC_CACHE_NULL, memoryCacheKey); return; } } OutputStream os = new BufferedOutputStream(new FileOutputStream(targetFile), BUFFER_SIZE); try { bmp.compress( configuration.imageCompressFormatForDiscCache, configuration.imageQualityForDiscCache, os); } finally { IoUtils.closeSilently(os); } bmp.recycle(); }