Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download an image using Glide via HTTP POST method

Server provides the API to load image through Post request, whose body looks like

{
 "image_id": someId,
 "session_id": "someId"
}
Response - stream.
How to download an image using Glide via HTTP POST method?
like image 302
Dmitry Isachenko Avatar asked Dec 27 '16 09:12

Dmitry Isachenko


People also ask

How do you download pictures from Glide?

load("YOUR_URL") . asBitmap() . into(new SimpleTarget<Bitmap>(100,100) { @Override public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { saveImage(resource); } }); Save the bitmap into your memory.

How do you download images from URL on Glide Android?

To simply load an image to LinearLayout, we call the with() method of Glide class and pass the context, then we call the load() method, which contains the URL of the image to be downloaded and finally we call the into() method to display the downloaded image on our ImageView.

Where does glide save images?

Google creates a separate link for each image which can be copied-and-pasted into your Glide app spreadsheet. The image picker is pretty basic. Images are stored in Firebase and limited to 10mb files.


1 Answers

Algorithm for loading images using Glide & Retrofit 2 via HTTP POST method:

1) Create interface, which should be implemented by all requests for image loading via HTTP POST method:

public interface CacheableRequest {
    String getCacheKey();
}

2) Create model to load, which will be used as a parameter for Glide.with(context).load(model):

import java.io.IOException;
import java.io.InputStream;

import okhttp3.ResponseBody;
import retrofit2.Call;

public class RetrofitStream {

    private final Call<ResponseBody> call;
    private final CacheableRequest request;

    public RetrofitStream(Call<ResponseBody> call, CacheableRequest request) {
        this.call = call;
        this.request = request;
    }

    public InputStream getStream() throws IOException {
        return call.execute().body().byteStream();
    }

    public String getCacheKey() {
        return request.getCacheKey();
    }
}

3) Create an implementation of com.bumptech.glide.load.data.DataFetcher:

import android.support.annotation.Nullable;

import com.bumptech.glide.Priority;
import com.bumptech.glide.load.data.DataFetcher;

import java.io.IOException;
import java.io.InputStream;

public class RetrofitFetcher implements DataFetcher<InputStream> {

    private final RetrofitStream retrofitStream;
    private InputStream stream;

    public RetrofitFetcher(RetrofitStream retrofitStream) {
        this.retrofitStream = retrofitStream;
    }

    @Nullable
    @Override
    public InputStream loadData(Priority priority) throws Exception {
        return stream = retrofitStream.getStream();
    }

    @Override
    public void cleanup() {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException ignored) {
            }
        }
    }

    @Override
    public String getId() {
        return retrofitStream.getCacheKey();
    }

    @Override
    public void cancel() {

    }
}

4) Create an implementation of com.bumptech.glide.load.model.ModelLoader:

import android.content.Context;
import android.support.annotation.Keep;
import android.support.annotation.Nullable;

import com.bumptech.glide.load.data.DataFetcher;
import com.bumptech.glide.load.model.GenericLoaderFactory;
import com.bumptech.glide.load.model.ModelCache;
import com.bumptech.glide.load.model.ModelLoader;
import com.bumptech.glide.load.model.ModelLoaderFactory;

import java.io.InputStream;

public class RetrofitStreamLoader implements ModelLoader<RetrofitStream, InputStream> {

    private final ModelCache<RetrofitStream, RetrofitStream> modelCache;

    @Keep
    public RetrofitStreamLoader() {
        this(null);
    }

    public RetrofitStreamLoader(@Nullable ModelCache<RetrofitStream, RetrofitStream> modelCache) {
        this.modelCache = modelCache;
    }

    @Override
    public DataFetcher<InputStream> getResourceFetcher(RetrofitStream model, int width, int height) {
        // GlideUrls memoize parsed URLs so caching them saves a few object instantiations and time spent parsing urls.
        RetrofitStream stream = model;
        if (modelCache != null) {
            stream = modelCache.get(model, 0, 0);
            if (stream == null) {
                modelCache.put(model, 0, 0, model);
                stream = model;
            }
        }
        return new RetrofitFetcher(stream);
    }

    /**
     * The default factory for {@link RetrofitStreamLoader}s.
     */
    public static class Factory implements ModelLoaderFactory<RetrofitStream, InputStream> {

        private final ModelCache<RetrofitStream, RetrofitStream> modelCache = new ModelCache<>(500);

        @Override
        public ModelLoader<RetrofitStream, InputStream> build(Context context, GenericLoaderFactory factories) {
            return new RetrofitStreamLoader(modelCache);
        }

        @Override
        public void teardown() {
            // Do nothing.
        }
    }
}

5) Create an implementation of GlideModule, which allows configure Glide

import android.content.Context;

import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.cache.InternalCacheDiskCacheFactory;
import com.bumptech.glide.module.GlideModule;

import java.io.InputStream;

public class RetrofitGlideModule implements GlideModule {

    private final static int IMAGES_CACHE_MAX_BYTE_SIZE = 20 * 1024 * 1024;
    private final static String IMAGES_CACHE_PATH = "images";

    @Override
    public void applyOptions(Context context, GlideBuilder builder) {
        builder.setDiskCache(new InternalCacheDiskCacheFactory(context, IMAGES_CACHE_PATH, IMAGES_CACHE_MAX_BYTE_SIZE))
                .setDecodeFormat(DecodeFormat.PREFER_RGB_565);
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        glide.register(RetrofitStream.class, InputStream.class, new RetrofitStreamLoader.Factory());
    }
}

6) Add meta data about created RetrofitGlideModule in AndroidManifest.xml

<application...>
<meta-data
        android:name="<package>.RetrofitGlideModule"
        android:value="GlideModule" />
</application>

Now you can download images next way:

Call<ResponseBody> call = retrofit.getImage(cacheableRequest);
Glide.with(context).load(new RetrofitStream(call, cacheableRequest)).into(imageView);
like image 69
Dmitry Isachenko Avatar answered Nov 15 '22 09:11

Dmitry Isachenko