@Override public Result load(Request request, int networkPolicy) throws IOException { JcifsFileEditor editor = new JcifsFileEditor(request.uri); InputStream inputStream = null; try { inputStream = editor.getInputStream(); } catch (IOException e) { Log.e(TAG, "Failed to get the input stream for "+request.uri, e); return null; } Bitmap bitmap = BitmapFactory.decodeStream(inputStream); inputStream.close(); if (bitmap==null) { return null; } else { return new Result(bitmap, Picasso.LoadedFrom.NETWORK); } }
/** * Copy/Paste of com.squareup.picasso.RequestHandler.calculateInSampleSize()... */ static void calculateInSampleSize(int reqWidth, int reqHeight, int width, int height, BitmapFactory.Options options, Request request) { int sampleSize = 1; if (height > reqHeight || width > reqWidth) { final int heightRatio; final int widthRatio; if (reqHeight == 0) { sampleSize = (int) Math.floor((float) width / (float) reqWidth); } else if (reqWidth == 0) { sampleSize = (int) Math.floor((float) height / (float) reqHeight); } else { heightRatio = (int) Math.floor((float) height / (float) reqHeight); widthRatio = (int) Math.floor((float) width / (float) reqWidth); sampleSize = request.centerInside ? Math.max(heightRatio, widthRatio) : Math.min(heightRatio, widthRatio); } } options.inSampleSize = sampleSize; options.inJustDecodeBounds = false; }
@Override public Result load(Request request, int networkPolicy) throws IOException { String cardId = request.uri.getHost(); Picasso.LoadedFrom loadedFrom = Picasso.LoadedFrom.DISK; InputStream inputStream = ArcaneTrackerApplication.getContext().getAssets().open("bars/" + cardId + ".webp"); if (inputStream == null) { throw new IOException(); } Bitmap b = BitmapFactory.decodeStream(inputStream); inputStream.close(); return new Result(b, loadedFrom); }
@Override public Result load(Request request, int networkPolicy) throws IOException { // do whatever is necessary here to load the image // important: you can only return a Result object // the constructor takes either a Bitmap or InputStream object, nothing else! // get the key for the requested image // if the schema is "eatfoody://cupcake", it'd return "cupcake" String imageKey = request.uri.getHost(); Bitmap bitmap; if (imageKey.contentEquals("cupcake")) { bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cupcake); } else if (imageKey.contentEquals("full_cake")) { bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.full_cake); } else { // fallback image bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); } // return the result with the bitmap and the source info return new Result(bitmap, Picasso.LoadedFrom.DISK); }
@Override public Request transformRequest(Request request) { if (request.resourceId != 0) { return request; // Don't transform resource requests. } Uri uri = request.uri; String scheme = uri.getScheme(); if (!"https".equals(scheme) && !"http".equals(scheme)) { return request; // Thumbor only supports remote images. } if (!request.hasSize()) { return request; // Thumbor only works with resizing images. } // Start building a new request for us to mutate. Request.Builder newRequest = request.buildUpon(); // Create the url builder to use. ThumborUrlBuilder urlBuilder = thumbor.buildImage(uri.toString()); // Resize the image to the target size. urlBuilder.resize(request.targetWidth, request.targetHeight); newRequest.clearResize(); // If the center inside flag is set, perform that with Thumbor as well. if (request.centerInside) { urlBuilder.fitIn(); newRequest.clearCenterInside(); } // If the Android version is modern enough use WebP for downloading. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { urlBuilder.filter(format(ImageFormat.WEBP)); } // Update the request with the completed Thumbor URL. newRequest.setUri(Uri.parse(urlBuilder.toUrl())); return newRequest.build(); }
@Test public void simpleResize() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build(); Request output = transformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); String expected = Thumbor.create(HOST).buildImage(IMAGE).resize(50, 50).toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Config(sdk = 18) @Test public void simpleResizeOnJbMr2UsesWebP() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build(); Request output = transformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); String expected = Thumbor.create(HOST) .buildImage(IMAGE) .resize(50, 50) .filter(format(ImageFormat.WEBP)) .toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Test public void simpleResizeWithCenterCrop() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerCrop().build(); Request output = transformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); assertThat(output.centerCrop).isFalse(); String expected = Thumbor.create(HOST).buildImage(IMAGE).resize(50, 50).toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Test public void simpleResizeWithCenterInside() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerInside().build(); Request output = transformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); assertThat(output.centerInside).isFalse(); String expected = Thumbor.create(HOST).buildImage(IMAGE).resize(50, 50).fitIn().toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Test public void simpleResizeWithEncryption() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).build(); Request output = secureTransformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); String expected = Thumbor.create(HOST, KEY).buildImage(IMAGE).resize(50, 50).toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Test public void simpleResizeWithCenterInsideAndEncryption() { Request input = new Request.Builder(IMAGE_URI).resize(50, 50).centerInside().build(); Request output = secureTransformer.transformRequest(input); assertThat(output).isNotSameAs(input); assertThat(output.hasSize()).isFalse(); assertThat(output.centerInside).isFalse(); String expected = Thumbor.create(HOST, KEY).buildImage(IMAGE).resize(50, 50).fitIn().toUrl(); assertThat(output.uri.toString()).isEqualTo(expected); }
@Override public RequestHandler.Result load(Request data, int arg1) throws IOException { long imageId = Long.parseLong(data.uri.getLastPathSegment()); Bitmap bm = Stores.getInstance() .localPhotos() .getImageThumbnail(imageId); return new RequestHandler.Result(bm, Picasso.LoadedFrom.DISK); }
@Override public Result load(Request request, int networkPolicy) throws IOException { final long videoId = Long.parseLong(request.uri.getHost()); if (videoId<0) { return null; } Bitmap thumbnail = VideoStore.Video.Thumbnails.getThumbnail(mContext.getContentResolver(), videoId, VideoStore.Video.Thumbnails.MINI_KIND, null, !"1".equals(request.uri.getQueryParameter("nothumbcreation"))); if (thumbnail==null) { return null; } return new RequestHandler.Result(thumbnail, Picasso.LoadedFrom.DISK); }
private static Bitmap decodeResource(Resources resources, int id, Request data) { final BitmapFactory.Options options = createBitmapOptions(data); if (requiresInSampleSize(options)) { BitmapFactory.decodeResource(resources, id, options); calculateInSampleSize(data.targetWidth, data.targetHeight, options, data); } return BitmapFactory.decodeResource(resources, id, options); }
/** * Copy/Paste of com.squareup.picasso.RequestHandler.createBitmapOptions()... */ static BitmapFactory.Options createBitmapOptions(Request data) { final boolean justBounds = data.hasSize(); final boolean hasConfig = data.config != null; BitmapFactory.Options options = null; if (justBounds || hasConfig) { options = new BitmapFactory.Options(); options.inJustDecodeBounds = justBounds; if (hasConfig) { options.inPreferredConfig = data.config; } } return options; }
@Override public Result load(Request request, int networkPolicy) throws IOException { String imagePath = request.uri.getPath().substring(1); // Grab only the path sans leading slash. // Check the disk cache for the image. A non-null return value indicates a hit. boolean cacheHit = emulatedDiskCache.get(imagePath) != null; // If there's a hit, grab the image stream and return it. if (cacheHit) { return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.DISK); } // If we are not allowed to hit the network and the cache missed return a big fat nothing. if (NetworkPolicy.isOfflineOnly(networkPolicy)) { return null; } // If we got this far there was a cache miss and hitting the network is required. See if we need // to fake an network error. if (behavior.calculateIsFailure()) { SystemClock.sleep(behavior.calculateDelay(MILLISECONDS)); throw new IOException("Fake network error!"); } // We aren't throwing a network error so fake a round trip delay. SystemClock.sleep(behavior.calculateDelay(MILLISECONDS)); // Since we cache missed put it in the LRU. AssetFileDescriptor fileDescriptor = assetManager.openFd(imagePath); long size = fileDescriptor.getLength(); fileDescriptor.close(); emulatedDiskCache.put(imagePath, size); // Grab the image stream and return it. return new Result(loadBitmap(imagePath), Picasso.LoadedFrom.NETWORK); }
@Override public Result load(Request request, int networkPolicy) throws IOException { Log.i(TAG, "load " + request.uri.toString()); StorageReference gsReference = firebaseStorage.getReferenceFromUrl(request.uri.toString()); StreamDownloadTask mStreamTask = gsReference.getStream(); InputStream inputStream; try { inputStream = Tasks.await(mStreamTask).getStream(); return new Result(BitmapFactory.decodeStream(inputStream), Picasso.LoadedFrom.NETWORK); } catch (ExecutionException | InterruptedException e) { throw new IOException(e); } }
@Override public Result load(Request request, int networkPolicy) throws IOException { String packageName = request.uri.getSchemeSpecificPart(); Drawable drawable; try { drawable = mPackageManager.getApplicationIcon(packageName); } catch (PackageManager.NameNotFoundException ignored) { return null; } Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); return new Result(bitmap, Picasso.LoadedFrom.DISK); }
@Override public boolean canHandleRequest(Request data) { if (data.uri.getScheme().equals("card")) { return true; } return false; }
@Override public boolean canHandleRequest(Request data) { if (data.uri.getScheme().equals("bar")) { return true; } return false; }
@Override public Result load(Request request, int networkPolicy) throws IOException { String path = request.uri.toString().replace(PICASSO_SCHEME_VIDEO + ":", ""); Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Video.Thumbnails.MICRO_KIND); return new Result(bitmap, Picasso.LoadedFrom.DISK); }
@Override public Result load(Request request, int networkPolicy) throws IOException { try { return new Result(getFullResIcon(request.uri.toString().split(":")[1]), DISK); } catch (PackageManager.NameNotFoundException e) { return null; } }
@Override public boolean canHandleRequest(Request data) { Uri uri = data.uri; if (!"layer".equals(uri.getScheme())) return false; List<String> segments = uri.getPathSegments(); if (segments.size() != 4) return false; if (!segments.get(2).equals("parts")) return false; return true; }
@Override public Result load(Request request, int networkPolicy) throws IOException { Queryable queryable = mLayerClient.get(request.uri); if (!(queryable instanceof MessagePart)) return null; MessagePart part = (MessagePart) queryable; if (part.isContentReady()) return new Result(part.getDataStream(), LoadedFrom.DISK); if (!Util.downloadMessagePart(mLayerClient, part, 3, TimeUnit.MINUTES)) return null; return new Result(part.getDataStream(), LoadedFrom.NETWORK); }
@Override public Result load(Request data, int networkPolicy) throws IOException { String path = getCoverPath(data.uri); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; return new Result(BitmapFactory.decodeFile(path, options), Picasso.LoadedFrom.DISK); }
@Override public Request transformRequest(Request request) { if (request.resourceId != 0) { return request; // Don't transform resource requests. } Uri uri = request.uri; String scheme = uri.getScheme(); if (!"https".equals(scheme) && !"http".equals(scheme)) { return request; // Thumbor only supports remote images. } if (!request.hasSize()) { return request; // Thumbor only works with resizing images. } // Start building a new request for us to mutate. Request.Builder newRequest = request.buildUpon(); // Create the url builder to use. ThumborUrlBuilder urlBuilder = thumbor.buildImage(uri.toString()); // Resize the image to the target size. urlBuilder.resize(request.targetWidth, request.targetHeight); newRequest.clearResize(); // If the center inside flag is set, perform that with Thumbor as well. if (request.centerInside) { urlBuilder.fitIn(); newRequest.clearCenterInside(); } // Update the request with the completed Thumbor URL. newRequest.setUri(Uri.parse(urlBuilder.toUrl())); return newRequest.build(); }
@Override public Result load(Request request, int networkPolicy) throws IOException { try { DbxDownloader<FileMetadata> downloader = mDbxClient.files().getThumbnailBuilder(request.uri.getPath()) .withFormat(ThumbnailFormat.JPEG) .withSize(ThumbnailSize.W1024H768) .start(); return new Result(downloader.getInputStream(), Picasso.LoadedFrom.NETWORK); } catch (DbxException e) { throw new IOException(e); } }
@Test public void resourceIdRequestsAreNotTransformed() { Request input = new Request.Builder(12).build(); Request output = transformer.transformRequest(input); assertThat(output).isSameAs(input); }
@Test public void nonHttpRequestsAreNotTransformed() { Request input = new Request.Builder(IMAGE_URI).build(); Request output = transformer.transformRequest(input); assertThat(output).isSameAs(input); }
@Test public void nonResizedRequestsAreNotTransformed() { Request input = new Request.Builder(IMAGE_URI).build(); Request output = transformer.transformRequest(input); assertThat(output).isSameAs(input); }
@Override public boolean canHandleRequest(Request data) { return data.uri != null && data.uri.getScheme() != null && data.uri.getScheme().equals("content"); }