0

I want to use Glide to download pictures from my Nextcloud server and load it into an ImageView. Nextcloud is providing an own library for connecting with the nextcloud server and downloading data. The standard call:

Glide.with(mContext)
   .load(new File(path))
   .into(holder.mImagePreview);

is not working because the file url is behind the authentication of the nextcloud server.

How can I pass my own downloading logic to Glide, so I can download pictures from my nextcloud server with the nextcloud library?

TmCrafz
  • 184
  • 1
  • 12

1 Answers1

2

You need to write custom Model Loader for Glide. this ducment describe how to write custom Model Loaders.


class FileDataFetcher(
        private val model: File
) : DataFetcher<ByteBuffer> {
    override fun getDataClass(): Class<ByteBuffer> = ByteBuffer::class.java

    override fun cleanup() {
        TODO("cleanup data fetcher | run on background thread")
    }

    override fun getDataSource(): DataSource {
        TODO("return DataSource.LOCAL or DataSource.LOCAL OR DataSource.MEMORY_CACHE or DataSource.REMOTE based on your need")
    }

    override fun cancel() {
        TODO("cancel download request | run on main thread")
    }

    override fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in ByteBuffer>) {
        TODO("start download or load in external storage | run on background thread")
    }
}
class FileModelLoader : ModelLoader<File, ByteBuffer> {

    override fun buildLoadData(model: File, width: Int, height: Int, options: Options): ModelLoader.LoadData<ByteBuffer>? =
            ModelLoader.LoadData(ObjectKey(model), FileDataFetcher(model))

    override fun handles(model: File): Boolean = model.exists()
}

class FileModelLoaderFactory : ModelLoaderFactory<File, ByteBuffer> {

    override fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<File, ByteBuffer> = FileModelLoader()

    override fun teardown() = Unit
}
@GlideModule
class GlideAppModule : AppGlideModule() {
    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        registry.prepend(File::class.java, ByteBuffer::class.java, FileModelLoaderFactory())
        super.registerComponents(context, glide, registry)
    }
}

And when you load File with Glide

Glide.with(mContext)
   .load(new File(path))
   .into(holder.mImagePreview);

Glide calls your Model Loader

Mehdi Yari
  • 481
  • 3
  • 12