@Nullable private InputStreamPipe openCacheInputStreamPipe(int index) { if (sCache == null) { return null; } String key = EhCacheKeyFactory.getImageKey(mGid, index); return sCache.getInputStreamPipe(key); }
private boolean copyFromCacheToDownloadDir(int index) { if (sCache == null) { return false; } UniFile dir = getDownloadDir(); if (dir == null) { return false; } // Find image file in cache String key = EhCacheKeyFactory.getImageKey(mGid, index); InputStreamPipe pipe = sCache.getInputStreamPipe(key); if (pipe == null) { return false; } OutputStream os = null; try { // Get extension String extension; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; pipe.obtain(); BitmapFactory.decodeStream(pipe.open(), null, options); pipe.close(); extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType); if (extension != null) { extension = '.' + extension; } else { return false; } // Fix extension extension = fixExtension(extension); // Copy from cache to download dir UniFile file = dir.createFile(generateImageFilename(index, extension)); if (file == null) { return false; } os = file.openOutputStream(); IOUtils.copy(pipe.open(), os); return true; } catch (IOException e) { return false; } finally { IOUtils.closeQuietly(os); pipe.close(); pipe.release(); } }