com.squareup.picasso.Request Java Examples

The following examples show how to use com.squareup.picasso.Request. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PicassoDataRequestHandler.java    From android with MIT License 6 votes vote down vote up
@Override
public Result load(Request request, int networkPolicy) {
    String uri = request.uri.toString();
    String imageDataBytes = uri.substring(uri.indexOf(",") + 1);
    byte[] bytes = Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    if (bitmap == null) {
        String show = uri.length() > 50 ? uri.substring(0, 49) + "..." : uri;
        RuntimeException malformed = new RuntimeException("Malformed data uri: " + show);
        Log.e("Could not load image", malformed);
        throw malformed;
    }

    return new Result(bitmap, Picasso.LoadedFrom.NETWORK);
}
 
Example #2
Source File: ImageRequestTransformer.java    From cathode with Apache License 2.0 6 votes vote down vote up
@Override public Request transformRequest(Request request) {
  Uri uri = request.uri;

  if (uri == null) {
    return request;
  }

  final String scheme = uri.getScheme();

  if (!SCHEMES.contains(scheme)) {
    return request;
  }

  final ImageType imageType = ImageType.fromValue(uri.getHost());
  final String size = ImageSizeSelector.getInstance(context)
      .getSize(imageType, request.targetWidth, request.targetHeight);

  Uri newUri = uri.buildUpon().appendQueryParameter(QUERY_SIZE, size).build();

  return request.buildUpon().setUri(newUri).build();
}
 
Example #3
Source File: PicassoHook.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
/**
 * 注入到com.squareup.picasso.Request 构造方法中
 */
public static void proxy(Object request) {
    try {
        if (request instanceof Request) {
            Request requestObj = (Request) request;
            List<Transformation> transformations = requestObj.transformations;
            if (transformations == null) {
                transformations = new ArrayList<>();
                transformations.add(new DokitPicassoTransformation(requestObj.uri, requestObj.resourceId));
            } else {
                transformations.clear();
                transformations.add(new DokitPicassoTransformation(requestObj.uri, requestObj.resourceId));
            }
            ReflectUtils.reflect(request).field("transformations", transformations);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
 
Example #4
Source File: ImageRequestHandler.java    From cathode with Apache License 2.0 6 votes vote down vote up
@Override public RequestHandler.Result load(Request request, int networkPolicy)
    throws IOException {
  final String baseUrl = getBaseUrl();
  if (TextUtils.isEmpty(baseUrl)) {
    return null;
  }

  String path = transform(request, request.uri);

  if (TextUtils.isEmpty(path)) {
    return null;
  }

  Downloader.Response response = downloader.load(Uri.parse(path), networkPolicy);
  if (response == null) {
    return null;
  }

  InputStream is = response.getInputStream();
  if (is == null) {
    return null;
  }

  return new RequestHandler.Result(is, Picasso.LoadedFrom.NETWORK);
}
 
Example #5
Source File: AppWhitelistAdapter.java    From SABS with MIT License 5 votes vote down vote up
@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);
}
 
Example #6
Source File: MockRequestHandler.java    From u2020-mvp with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #7
Source File: MockRequestHandler.java    From u2020 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #8
Source File: LocalCoverHandler.java    From bubble with MIT License 5 votes vote down vote up
@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);
}
 
Example #9
Source File: AppIconRequestHandler.java    From AndroidProcesses with Apache License 2.0 5 votes vote down vote up
@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;
  }
}
 
Example #10
Source File: FirebaseRequestHandler.java    From android-instant-apps-demo with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #11
Source File: VideoRequestHandler.java    From PictureSelector with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public Result load(Request request, int networkPolicy) throws IOException {
    Uri uri = request.uri;
    String path = uri.getPath();
    if (!TextUtils.isEmpty(path)) {
        Bitmap bm = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
        return new Result(bm, Picasso.LoadedFrom.DISK);
    }
    return null;
}
 
Example #12
Source File: BaseUrlRequestHandler.java    From cathode with Apache License 2.0 5 votes vote down vote up
protected String transform(Request request, Uri uri) throws IOException {
  final String image = uri.getPath();
  String size = request.uri.getQueryParameter(QUERY_SIZE);

  String url = getBaseUrl() + size + image;
  Timber.d("Url: %s", url);
  return url;
}
 
Example #13
Source File: PackageDisablerFragment.java    From SABS with MIT License 5 votes vote down vote up
@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);
}
 
Example #14
Source File: GameBannerRequestHandler.java    From citra_android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Result load(Request request, int networkPolicy)
{
  String url = request.uri.getHost() + request.uri.getPath();
  int[] vector = NativeLibrary.GetBanner(url);
  int width = 48;
  int height = 48;
  Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
  bitmap.copyPixelsFromBuffer(IntBuffer.wrap(vector));
  return new Result(bitmap, Picasso.LoadedFrom.DISK);
}
 
Example #15
Source File: AppWhitelistAdapter.java    From notSABS with MIT License 5 votes vote down vote up
@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);
}
 
Example #16
Source File: PackageDisablerFragment.java    From notSABS with MIT License 5 votes vote down vote up
@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);
}
 
Example #17
Source File: FileThumbnailRequestHandler.java    From dropbox-sdk-java with MIT License 5 votes vote down vote up
@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(Okio.source(downloader.getInputStream()), Picasso.LoadedFrom.NETWORK);
    } catch (DbxException e) {
        throw new IOException(e);
    }
}
 
Example #18
Source File: LocalCoverHandler.java    From bubble with MIT License 4 votes vote down vote up
@Override
public boolean canHandleRequest(Request data) {
    return HANDLER_URI.equals(data.uri.getScheme());
}
 
Example #19
Source File: PersonRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return ImageUri.ITEM_PERSON.equals(data.uri.getScheme());
}
 
Example #20
Source File: MovieRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return ImageUri.ITEM_MOVIE.equals(data.uri.getScheme());
}
 
Example #21
Source File: ImageRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return ImageUri.ITEM_IMAGE.equals(data.uri.getScheme());
}
 
Example #22
Source File: ShowRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return ImageUri.ITEM_SHOW.equals(data.uri.getScheme());
}
 
Example #23
Source File: SeasonRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return ImageUri.ITEM_SEASON.equals(data.uri.getScheme());
}
 
Example #24
Source File: EpisodeRequestHandler.java    From cathode with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return ImageUri.ITEM_EPISODE.equals(data.uri.getScheme());
}
 
Example #25
Source File: FileThumbnailRequestHandler.java    From dropbox-sdk-java with MIT License 4 votes vote down vote up
@Override
public boolean canHandleRequest(Request data) {
    return SCHEME.equals(data.uri.getScheme()) && HOST.equals(data.uri.getHost());
}
 
Example #26
Source File: MockRequestHandler.java    From u2020 with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return "mock".equals(data.uri.getScheme());
}
 
Example #27
Source File: MockRequestHandler.java    From u2020-mvp with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
    return "mock".equals(data.uri.getScheme());
}
 
Example #28
Source File: LocalComicHandler.java    From bubble with MIT License 4 votes vote down vote up
@Override
public Result load(Request request, int networkPolicy) throws IOException {
    int pageNum = Integer.parseInt(request.uri.getFragment());
    InputStream stream = mParser.getPage(pageNum);
    return new Result(stream, Picasso.LoadedFrom.DISK);
}
 
Example #29
Source File: LocalComicHandler.java    From bubble with MIT License 4 votes vote down vote up
@Override
public boolean canHandleRequest(Request request) {
    return HANDLER_URI.equals(request.uri.getScheme());
}
 
Example #30
Source File: AppIconRequestHandler.java    From AndroidProcesses with Apache License 2.0 4 votes vote down vote up
@Override public boolean canHandleRequest(Request data) {
  return data.uri != null && TextUtils.equals(data.uri.getScheme(), SCHEME_PNAME);
}