/** * 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 com.bumptech.glide.request.RequestOptions#override(int, int)} if * previously called. * @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be * overridden by * {@link com.bumptech.glide.request.RequestOptions#override(int, int)}} if * previously called). */ public FutureTarget<TranscodeType> submit(int width, int height) { final RequestFutureTarget<TranscodeType> target = new RequestFutureTarget<>(context.getMainHandler(), width, height); if (Util.isOnBackgroundThread()) { context.getMainHandler().post(new Runnable() { @Override public void run() { if (!target.isCancelled()) { into(target); } } }); } else { into(target); } return target; }
/** * Tests b/69361054. */ @Test public void clear_withNonOwningRequestManager_onBackgroundTHread_doesNotThrow() { concurrency.runOnMainThread(new Runnable() { @Override public void run() { requestManager.onDestroy(); } }); final FutureTarget<Drawable> target = concurrency.wait(requestManager.load(raw.canonical).submit()); concurrency.runOnMainThread(new Runnable() { @Override public void run() { Glide.with(context).clear(target); } }); }
@Test public void submit_withDisabledMemoryCache_andResourceInActiveResources_loadsFromMemory() { Glide.init( context, new GlideBuilder().setMemoryCache(new MemoryCacheAdapter())); FutureTarget<Drawable> first = GlideApp.with(context) .load(raw.canonical) .submit(); concurrency.get(first); concurrency.get( GlideApp.with(context) .load(ResourceIds.raw.canonical) .listener(requestListener) .submit()); verify(requestListener) .onResourceReady( anyDrawable(), any(), anyTarget(), eq(DataSource.MEMORY_CACHE), anyBoolean()); }
/** * 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 com.bumptech.glide.request.RequestOptions#override(int, int)} if * previously called. * @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be * overridden by * {@link com.bumptech.glide.request.RequestOptions#override(int, int)}} if * previously called). */ public FutureTarget<TranscodeType> submit(int width, int height) { final RequestFutureTarget<TranscodeType> target = new RequestFutureTarget<>(glideContext.getMainHandler(), width, height); if (Util.isOnBackgroundThread()) { glideContext.getMainHandler().post(new Runnable() { @Override public void run() { if (!target.isCancelled()) { into(target, target); } } }); } else { into(target, target); } return target; }
public void run() { for (int n = 0; n < original_list.size(); n++) { //WORKAROUND TO PRE-CACHE ICONS try { FutureTarget<GlideDrawable> future = Glide .with(context) .load("http://onepiece-treasurecruise.com/wp-content/uploads/f" + convertID(n + 1) + ".png") .dontTransform() .override(thumbnail_width, thumbnail_height) .diskCacheStrategy(DiskCacheStrategy.RESULT) .into(thumbnail_width, thumbnail_height); GlideDrawable cacheFile = future.get(); } catch (Exception e) { Log.e("ERR", "Pic not found"); } } }
/** * Glide 获得图片缓存路径 */ private String getImagePath(String imgUrl) { String path = null; FutureTarget<File> future = Glide.with(ViewBigImageActivity.this) .load(imgUrl) .downloadOnly(500, 500); try { File cacheFile = future.get(); path = cacheFile.getAbsolutePath(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return path; }
@Test public void submit_withPreviousRequestClearedFromMemory_completesFromDataDiskCache() throws InterruptedException, ExecutionException, TimeoutException { FutureTarget<Drawable> future = GlideApp.with(context) .load(ResourceIds.raw.canonical) .diskCacheStrategy(DiskCacheStrategy.DATA) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS); concurrency.get(future); GlideApp.with(context).clear(future); clearMemoryCacheOnMainThread(); concurrency.get( GlideApp.with(context) .load(ResourceIds.raw.canonical) .diskCacheStrategy(DiskCacheStrategy.DATA) .listener(requestListener) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS)); verify(requestListener) .onResourceReady( anyDrawable(), any(), anyTarget(), eq(DataSource.DATA_DISK_CACHE), anyBoolean()); }
@Test public void submit_withPreviousButNoLongerReferencedIdenticalRequest_doesNotRecycleBitmap() throws InterruptedException, TimeoutException, ExecutionException { // We can't allow any mocks (RequestListener, Target etc) to reference this request or the test // will fail due to the transient strong reference to the request. Bitmap bitmap = concurrency.get( GlideApp.with(context) .asBitmap() .load(ResourceIds.raw.canonical) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS)); // Force the collection of weak references now that the listener/request in the first load is no // longer referenced. Runtime.getRuntime().gc(); concurrency.pokeMainThread(); FutureTarget<Bitmap> future = GlideApp.with(context) .asBitmap() .load(ResourceIds.raw.canonical) .diskCacheStrategy(DiskCacheStrategy.RESOURCE) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS); concurrency.get(future); Glide.with(context).clear(future); clearMemoryCacheOnMainThread(); BitmapSubject.assertThat(bitmap).isNotRecycled(); }
@Test public void clearDiskCache_doesNotPreventFutureLoads() throws ExecutionException, InterruptedException, TimeoutException { FutureTarget<Drawable> future = GlideApp.with(context) .load(ResourceIds.raw.canonical) .diskCacheStrategy(DiskCacheStrategy.DATA) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS); concurrency.get(future); GlideApp.with(context).clear(future); clearMemoryCacheOnMainThread(); GlideApp.get(context).clearDiskCache(); future = GlideApp.with(context) .load(ResourceIds.raw.canonical) .diskCacheStrategy(DiskCacheStrategy.DATA) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS); concurrency.get(future); GlideApp.with(context).clear(future); clearMemoryCacheOnMainThread(); concurrency.get( GlideApp.with(context) .load(ResourceIds.raw.canonical) .listener(requestListener) .diskCacheStrategy(DiskCacheStrategy.DATA) .submit(IMAGE_SIZE_PIXELS, IMAGE_SIZE_PIXELS)); verify(requestListener) .onResourceReady( anyDrawable(), any(), anyTarget(), eq(DataSource.DATA_DISK_CACHE), anyBoolean()); }
/** * 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; }
public void sharePic(final Context context, final String picUrl) { Observable.just(picUrl) .subscribeOn(Schedulers.newThread()) .map(new Func1<String, Uri>() { @Override public Uri call(String url) { FutureTarget<File> future = Glide.with(mContext) .load(url) .downloadOnly(500, 500); File cacheFile = null; try { cacheFile = future.get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } return Uri.fromFile(cacheFile); } }) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Action1<Uri>() { @Override public void call(Uri uri) { Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_STREAM, uri); sendIntent.setType("image/*"); context.startActivity(Intent.createChooser(sendIntent, "发送神♂秘图片")); } }); }
@SuppressWarnings("ThrowableResultOfMethodCallIgnored") @Override protected Result doInBackground(String... params) { @SuppressWarnings("unchecked") 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; }
@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; } }
public static File getAvatarFile(Context ctx, String avatarUrl) { File f = null; try { FutureTarget<File> future = Glide.with(ctx) .load(avatarUrl) .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); f = future.get(); Glide.clear(future); } catch (Exception e) { Logger.e(e); } return f; }
@Test public void submit_withRequestClearedFromMemory_doesNotLoadFromMemory() { Glide.init( context, new GlideBuilder().setMemoryCache(new MemoryCacheAdapter())); FutureTarget<Drawable> first = GlideApp.with(context) .load(raw.canonical) .submit(); concurrency.get(first); // Allow first to be GCed and removed from active resources. //noinspection UnusedAssignment first = null; // De-flake by allowing multiple tries for (int j = 0; j < 10; j++) { Runtime.getRuntime().gc(); concurrency.pokeMainThread(); try { // Loading again here won't shuffle our resource around because it only changes our // reference count from 1 to 2 and back. The reference we're waiting for will only be // decremented when the target is GCed. FutureTarget<Drawable> target = concurrency.wait( GlideApp.with(context) .load(ResourceIds.raw.canonical) .onlyRetrieveFromCache(true) .diskCacheStrategy(DiskCacheStrategy.NONE) .submit()); GlideApp.with(context).clear(target); } catch (RuntimeException e) { // The item has been cleared from active resources. break; } } concurrency.get( GlideApp.with(context) .load(ResourceIds.raw.canonical) .listener(requestListener) .submit()); verify(requestListener) .onResourceReady( anyDrawable(), any(), anyTarget(), not(eq(DataSource.MEMORY_CACHE)), anyBoolean()); }
/** * 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 com.bumptech.glide.request.RequestOptions#override(int, int)} if * previously called. * @param height The desired height in pixels, or {@link Target#SIZE_ORIGINAL}. This will be * overridden by * {@link com.bumptech.glide.request.RequestOptions#override(int, int)}} if * previously called). * @see RequestManager#clear(Target) * * @deprecated Use {@link #submit(int, int)} instead. */ @Deprecated public FutureTarget<TranscodeType> into(int width, int height) { return submit(width, height); }
/** * Returns a future that can be used to do a blocking get on a background thread. * * <p>This method defaults to {@link Target#SIZE_ORIGINAL} for the width and the height. However, * since the width and height will be overridden by values passed to {@link * RequestOptions#override(int, int)}, this method can be used whenever {@link RequestOptions} * with override values are applied, or whenever you want to retrieve the image in its original * size. * * @see #submit(int, int) * @see #into(Target) */ public FutureTarget<TranscodeType> submit() { return submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL); }
/** * 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. * * @deprecated Use {@link RequestManager#downloadOnly()} and {@link #into(int, int)}. */ @Deprecated public FutureTarget<File> downloadOnly(int width, int height) { return getDownloadOnlyRequest().submit(width, height); }
/** * 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. * * @deprecated Use {@link RequestManager#downloadOnly()} and {@link #into(int, int)}. */ @Deprecated @CheckResult public FutureTarget<File> downloadOnly(int width, int height) { return getDownloadOnlyRequest().submit(width, height); }
/** * Cancel the image loading request * * @param target Previously returned {@link Target} from {@link ImageLoader}.load... */ public static <R> void cancel(FutureTarget<R> target) { Glide.clear(target); }