com.bumptech.glide.request.FutureTarget Java Examples

The following examples show how to use com.bumptech.glide.request.FutureTarget. 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: GenericRequestBuilder.java    From giffun with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a future that can be used to do a blocking get on a background thread.
 *
 * @param width The desired width in pixels, or {@link Target#SIZE_ORIGINAL}. This will be overridden by
 *             {@link #override * (int, int)} if previously called.
 * @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be overridden by
 *              {@link #override * (int, int)}} if previously called).
 *
 * @see Glide#clear(FutureTarget)
 *
 * @return An {@link FutureTarget} that can be used to obtain the
 *         resource in a blocking manner.
 */
public FutureTarget<TranscodeType> into(int width, int height) {
    final RequestFutureTarget<ModelType, TranscodeType> target =
            new RequestFutureTarget<ModelType, TranscodeType>(glide.getMainHandler(), width, height);

    // TODO: Currently all loads must be started on the main thread...
    glide.getMainHandler().post(new Runnable() {
        @Override
        public void run() {
            if (!target.isCancelled()) {
                into(target);
            }
        }
    });

    return target;
}
 
Example #2
Source File: CompetitionNotificationHandler.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
public static void addNotificationToTray(final Context context, final String photoUrl, final PendingIntent pendingIntent, final String title, final String content) {
  try {
    FutureTarget<Bitmap> futureTarget = Glide.with(context)
      .asBitmap()
      .load(photoUrl)
      .submit();
    final Bitmap bitmap = futureTarget.get();
    Glide.with(context).clear(futureTarget);
    postNotificationWithBitmap(context,
      bitmap,
      title,
      content,
      pendingIntent);
  }
  catch (Exception e) {
    e.printStackTrace();
    postNotificationWithBitmap(context,
      null,
      title,
      content,
      pendingIntent);
  }
}
 
Example #3
Source File: Utils.java    From SAI with GNU General Public License v3.0 6 votes vote down vote up
public static File saveImageFromUriAsPng(Context context, Uri imageUri) throws Exception {
    FutureTarget<Bitmap> target = Glide.with(context)
            .asBitmap()
            .load(imageUri)
            .submit();

    try {
        Bitmap bitmap = target.get();

        File tempFile = createTempFileInCache(context, "Utils.saveImageFromUriAsPng", "png");
        if (tempFile == null) {
            throw new IOException("Unable to create file for image");
        }

        try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
        }

        return tempFile;
    } finally {
        Glide.with(context).clear(target);
    }
}
 
Example #4
Source File: SteemActionsNotificationHandler.java    From 1Rramp-Android with MIT License 5 votes vote down vote up
public static void addNotificationToTray(final Context context, final String photoUrl, final PendingIntent pendingIntent, final String title, final String content) {
  try {
    FutureTarget<Bitmap> futureTarget = Glide.with(context)
      .asBitmap()
      .load(photoUrl)
      .submit();
    final Bitmap bitmap = futureTarget.get();
    Glide.with(context).clear(futureTarget);
    postNotificationWithBitmap(context, bitmap, title, content, pendingIntent);
  }
  catch (Exception e) {
    e.printStackTrace();
    postNotificationWithBitmap(context, null, title, content, pendingIntent);
  }
}
 
Example #5
Source File: MainActivity.java    From music_player with Open Software License 3.0 5 votes vote down vote up
public void download_diaplay_Image(final String url) {
    try {
        final Context context = getApplicationContext();
        FutureTarget<File> target = Glide.with(context).load(url).downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
        Glide.with(context)
                .load(url)
                .placeholder(R.drawable.default_album)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        match_album.setImageDrawable(resource);
                        musicInfo musicNow = musicInfoArrayList.get(match_position);
                        musicNow.setAlbumLink(url);
                        MyApplication.getBoxStore().boxFor(musicInfo.class).put(musicNow);
                        new Handler().postDelayed(new Runnable() {//延迟执行
                            public void run() {
                                next_match();
                            }
                        }, 200);

                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: ImageLoader.java    From qvod with MIT License 5 votes vote down vote up
/**
 * Glide 获得图片缓存路径
 */
public static String getImagePath(String imgUrl) {
    String path = null;
    FutureTarget<File> future = Glide.with(VideoApplication.getInstance())
            .load(imgUrl)
            .downloadOnly(500, 500);
    try {
        File cacheFile = future.get();
        path = cacheFile.getAbsolutePath();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return path;
}
 
Example #7
Source File: Downloader.java    From glide-support with The Unlicense 5 votes vote down vote up
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
@Override protected Result doInBackground(String... params) {
	@SuppressWarnings({"unchecked", "rawtypes"})
	FutureTarget<File>[] requests = new FutureTarget[params.length];
	// fire everything into Glide queue
	for (int i = 0; i < params.length; i++) {
		if (isCancelled()) {
			break;
		}
		requests[i] = glide
				.load(params[i])
				.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
		;
	}
	// wait for each item
	Result result = new Result();
	for (int i = 0; i < params.length; i++) {
		if (isCancelled()) {
			for (int j = i; j < params.length; j++) {
				if (requests[i] != null) {
					Glide.clear(requests[i]);
				}
				result.failures.put(params[j], new CancellationException());
			}
			break;
		}
		try {
			File file = requests[i].get(10, TimeUnit.SECONDS);
			result.success.put(params[i], file);
		} catch (Exception e) {
			result.failures.put(params[i], e);
		} finally {
			Glide.clear(requests[i]);
		}
		publishProgress(params[i]);
	}
	return result;
}
 
Example #8
Source File: Downloader.java    From glide-support with The Unlicense 5 votes vote down vote up
@SafeVarargs
@Override protected final File doInBackground(FutureTarget<File>... params) {
	try {
		File file = params[0].get();
		File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
		File result = new File(dir, targetName);
		Utils.copy(file, result);
		return result;
	} catch (Exception e) {
		return null;
	}
}
 
Example #9
Source File: LinkPreviewRepository.java    From mollyim-android with GNU General Public License v3.0 4 votes vote down vote up
private @NonNull RequestController fetchThumbnail(@NonNull Context context, @NonNull String imageUrl, @NonNull Callback<Optional<Attachment>> callback) {
  FutureTarget<Bitmap> bitmapFuture = GlideApp.with(context).asBitmap()
                                                            .load(new ChunkedImageUrl(imageUrl))
                                                            .skipMemoryCache(true)
                                                            .diskCacheStrategy(DiskCacheStrategy.NONE)
                                                            .centerInside()
                                                            .submit(1024, 1024);

  RequestController controller = () -> bitmapFuture.cancel(false);

  SignalExecutors.UNBOUNDED.execute(() -> {
    try {
      Bitmap                bitmap = bitmapFuture.get();
      ByteArrayOutputStream baos = new ByteArrayOutputStream();

      bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);

      byte[]               bytes     = baos.toByteArray();
      Uri                  uri       = BlobProvider.getInstance().forData(bytes).createForSingleSessionInMemory();
      Optional<Attachment> thumbnail = Optional.of(new UriAttachment(uri,
                                                                     uri,
                                                                     MediaUtil.IMAGE_JPEG,
                                                                     AttachmentDatabase.TRANSFER_PROGRESS_STARTED,
                                                                     bytes.length,
                                                                     bitmap.getWidth(),
                                                                     bitmap.getHeight(),
                                                                     null,
                                                                     null,
                                                                     false,
                                                                     false,
                                                                     null,
                                                                     null,
                                                                     null,
                                                                     null,
                                                                     null));

      callback.onComplete(thumbnail);
    } catch (CancellationException | ExecutionException | InterruptedException e) {
      controller.cancel();
      callback.onComplete(Optional.absent());
    } finally {
      bitmapFuture.cancel(false);
    }
  });

  return () -> bitmapFuture.cancel(true);
}
 
Example #10
Source File: GenericTranscodeRequest.java    From giffun with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public FutureTarget<File> downloadOnly(int width, int height) {
    return getDownloadOnlyRequest().into(width, height);
}
 
Example #11
Source File: DrawableTypeRequest.java    From giffun with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public FutureTarget<File> downloadOnly(int width, int height) {
    return getDownloadOnlyRequest().downloadOnly(width, height);
}
 
Example #12
Source File: Glide.java    From giffun with Apache License 2.0 2 votes vote down vote up
/**
 * Cancel any pending loads Glide may have for the target and free any resources that may have been loaded into
 * the target so they may be reused.
 *
 * @param target The target to cancel loads for.
 */
public static void clear(FutureTarget<?> target) {
    target.clear();
}
 
Example #13
Source File: DownloadOptions.java    From giffun with Apache License 2.0 2 votes vote down vote up
/**
 * Loads the original unmodified data into the cache and returns a {@link java.util.concurrent.Future} that can be
 * used to retrieve the cache File containing the data.
 *
 * @param width The width in pixels to use to fetch the data.
 * @param height The height in pixels to use to fetch the data.
 * @return A {@link java.util.concurrent.Future} that can be used to retrieve the cache File containing the data.
 */
FutureTarget<File> downloadOnly(int width, int height);
 
Example #14
Source File: ImageLoader.java    From aptoide-client-v8 with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Cancel the image loading request
 *
 * @param target Previously returned {@link Target} from {@link ImageLoader}.load...
 */
public static <R> void cancel(Context context, FutureTarget<R> target) {
  Glide.with(context)
      .clear(target);
}