我有来自一些在base64中编码的SOAP API的图像.要使用GlIDe直接加载它我决定编写自定义ModelLoader.
滑翔版:4.3.1
滑行载重线:
GlIDeApp.with(activity) .load(Data().apply { code = licensePlateData.licensePlateimgID }) .into(vIEw.imagelicense)
GlIDeModule:
@GlIDeModuleclass MyAppGlIDeModule : AppGlIDeModule() { overrIDe fun registerComponents(context: Context, glIDe: GlIDe, registry: Registry) { super.registerComponents(context, glIDe, registry) val app = context.applicationContext as MyApplication registry.append(Data::class.java, ByteArray::class.java, MyImageLoaderFactory(app.API)) }}
装载机:
class MyImageLoaderFactory(private val API: API) : ModelLoaderFactory<Data, ByteArray> { overrIDe fun teardown() { } overrIDe fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<Data, ByteArray> { return MyImageLoader(API) }}class MyImageLoader(private val API: API) : ModelLoader<Data, ByteArray> { overrIDe fun buildLoadData(model: Data, wIDth: Int, height: Int, options: Options): ModelLoader.LoadData<ByteArray> { val key = "code:${model.code};wIDth:$wIDth;height:$height" return ModelLoader.LoadData<ByteArray>(ObjectKey(key), MyImageDataFetcher(API, Ge@R_419_5411@Request().apply { data = model })) } overrIDe fun handles(model: Data): Boolean { return true }}class MyImageDataFetcher(private val API: API, private val request: Ge@R_419_5411@Request) : DataFetcher<ByteArray> { private val disposables = Compositedisposable() overrIDe fun cleanup() { disposables.clear() } overrIDe fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in ByteArray>) { API.ge@R_419_5411@(request) //soap request .map { it.image.decodeBase64() } //translate String to ByteArray .subscribe({ callback.onDataReady(it) }, { if (it is Exception) { callback.onLoadFailed(it) } else { callback.onLoadFailed(MyException(it)) } }) .addTo(disposables) } overrIDe fun cancel() { disposables.clear() } overrIDe fun getDataClass(): Class<ByteArray> { return ByteArray::class.java } overrIDe fun getDataSource(): DataSource { return DataSource.REMOTE }}
堆栈跟踪/ LogCat:
com.bumptech.glIDe.Registry$NoSourceEncoderAvailableException: Failed to find source encoder for data class: class [B at com.bumptech.glIDe.Registry.getSourceEncoder(Registry.java:534) at com.bumptech.glIDe.load.engine.DecodeHelper.getSourceEncoder(DecodeHelper.java:232) at com.bumptech.glIDe.load.engine.sourceGenerator.cacheData(SourceGenerator.java:74) at com.bumptech.glIDe.load.engine.sourceGenerator.startNext(SourceGenerator.java:45) at com.bumptech.glIDe.load.engine.DecodeJob.runGenerators(DecodeJob.java:298) at com.bumptech.glIDe.load.engine.DecodeJob.runWrapped(DecodeJob.java:268) at com.bumptech.glIDe.load.engine.DecodeJob.run(DecodeJob.java:229) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) at java.lang.Thread.run(Thread.java:764) at com.bumptech.glIDe.load.engine.executor.GlIDeExecutor$DefaultThreadFactory.run(GlIDeExecutor.java:413)
我认为我错过了让它发挥作用的东西.
解决方法:
感谢https://github.com/bumptech/glide/issues/2677的评论,我找到了解决方案.
@GlIDeModuleclass MyAppGlIDeModule : AppGlIDeModule() { overrIDe fun registerComponents(context: Context, glIDe: GlIDe, registry: Registry) { super.registerComponents(context, glIDe, registry) val app = context.applicationContext as MyApplication registry.append(Data::class.java, ByteBuffer::class.java, MyImageLoaderFactory(app.API)) }}class MyImageLoaderFactory(private val API: API) : ModelLoaderFactory<Data, ByteBuffer> { overrIDe fun teardown() { } overrIDe fun build(multiFactory: MultiModelLoaderFactory): ModelLoader<Data, ByteBuffer> { return MyImageLoader(API) }}class MyImageLoader(private val API: API) : ModelLoader<Data, ByteBuffer> { overrIDe fun buildLoadData(model: Data, wIDth: Int, height: Int, options: Options): ModelLoader.LoadData<ByteBuffer> { val key = "code:${model.code};wIDth:$wIDth;height:$height" return ModelLoader.LoadData<ByteBuffer>(ObjectKey(key), MyImageDataFetcher(API, Ge@R_419_5411@Request().apply { data = model })) } overrIDe fun handles(model: Data): Boolean { return true }}class ClmImageDataFetcher(private val API: API, private val request: Ge@R_419_5411@Request) : DataFetcher<ByteBuffer> { private val disposables = Compositedisposable() overrIDe fun cleanup() { disposables.clear() } overrIDe fun loadData(priority: Priority, callback: DataFetcher.DataCallback<in ByteBuffer>) { API.ge@R_419_5411@(request) .map { it.image.decodeBase64() } .subscribe({ callback.onDataReady(ByteBuffer.wrap(it)) }, { if (it is Exception) { callback.onLoadFailed(it) } else { callback.onLoadFailed(MyException(it)) } }) .addTo(disposables) } overrIDe fun cancel() { disposables.clear() } overrIDe fun getDataClass(): Class<ByteBuffer> { return ByteBuffer::class.java } overrIDe fun getDataSource(): DataSource { return DataSource.REMOTE }}
总结 以上是内存溢出为你收集整理的java – 用于base64图像的Custom Glide ModelLoader全部内容,希望文章能够帮你解决java – 用于base64图像的Custom Glide ModelLoader所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)