Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sylyac2000/8c1634c26d5aa470c34c8a5114463b8a to your computer and use it in GitHub Desktop.
Save Sylyac2000/8c1634c26d5aa470c34c8a5114463b8a to your computer and use it in GitHub Desktop.

Using RxJava and Glide to download multiple GIFs

SOURCE

This guide is for RxJava 1 and Glide v3 (3.8.0).

Have a list of URLs to GIFs, how to download them all in separated threads and proceed when all GIFs are downloaded?

private void download(final List<String> urlList) {
        Observable observable = Observable.from(urlList)
                .flatMap(new Func1<String, Observable<File>>() {
                    @Override
                    public Observable<File> call(String url) {
                        return getDownloadObservable(url);
                    }
                });

        mSubscriptions.add(observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<File>() {
                    @Override
                    public void onCompleted() {
                        Timber.d("onCompleted(): DEBUG_GLIDE_DOWNLOAD");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.d("onError(): DEBUG_GLIDE_DOWNLOAD e: %s", e.getMessage());
                    }

                    @Override
                    public void onNext(File file) {
                        Timber.d("onNext(): DEBUG_GLIDE_DOWNLOAD file path: %s", file.getPath());
                    }
                }));
    }

    private Observable<File> getDownloadObservable(final String url){
        //need fromCallable as downloadOnly need to be put in a background thread
        //if not we can just use just as in the source
        return Observable.fromCallable(new Callable<File>() {
            @Override
            public File call() throws Exception {
                return Glide.with(mWeakContext.get())
                        .load(url)
                        .downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
                        .get();
            }
        });
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment