Our app supports both online and offline mechanism. That means the app should work and show the images regardless if there is an internet connection or not.
We're using Glide (4.2.0) library for images and everything works fine if there is an internet connection. We're using local database & sockets to synchronize data between client and server. Images are being sent via sockets encoded base64 and not with HTTP Service.
The problem appears if there is no internet connection and Glide's cache is empty. What I'm trying to achieve is to write the Bitmap
directly to Glide's cache (background thread):
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
try {
GlideApp.with(App.getInstance())
.load(byteArray)
.signature(new ObjectKey(objectId))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
And this is how the image is being loaded (UI thread):
GlideApp.with(this)
.load(URL + "/" + objectId + "/image")
.signature(new ObjectKey(user.getObjectId()))
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(imageView);