-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Load Encrypted Image from File Using Glide #1498
Comments
#912 has some details, but not full. Your implementation is a mini image loader, the only thing missing is the ability to cancel previous decrypts when the item is recycled and rebound. You have two options:
Based on your names you are dealing with encrypted local files, so caching shouldn't be an issue. Mostly because you don't need to cache local files as they would be just duped in Glide cache. Based on all this and your code sample I think you need something like this (using inner class notation, but you can and should extract separate classes: Glide
.with(context)
.load(getItem(position)) // custom load using registered factory
.diskCacheStrategy(DiskCacheStrategy.NONE) // file already on disk, don't save it again
//.override(100, 100) // getThumb(int thumbnailSize), but I suggest you let Glide figure the size out
.placeholder(R.drawable.img_placeholder)
.error(R.drawable.img_unavailable)
.into(holder.mImage);
// in GlideModule, see https://github.com/bumptech/glide/wiki/Configuration
glide.register(EncryptedFileContainer.class, InputStream.class, new ModelLoaderFactory<EncryptedFileContainer, InputStream>() {
@Override public ModelLoader<EncryptedFileContainer, InputStream> build(Context context, GenericLoaderFactory factories) {
return new StreamModelLoader<EncryptedFileContainer>() {
@Override public DataFetcher<InputStream> getResourceFetcher(final EncryptedFileContainer model, int width, int height) {
return new DataFetcher<InputStream>() {
private InputStream stream;
@Override public InputStream loadData(Priority priority) throws Exception {
// prefer throwing an exception to returning null!
return stream = model.getDecryptedThumb().readStream();
}
@Override public void cleanup() {
cancel();
}
@Override public @NonNull String getId() {
return model.getEncryptedFileDBModel().getOriginalFileName();
}
@Override public void cancel() {
if (stream != null) {
try {
stream.close(); // interrupts decode if any
stream = null;
} catch (IOException ignore) {
}
}
}
};
}
};
}
@Override public void teardown() {
// no resources to free in factory
}
}); Let me know if this is sufficient, or if I made a false assumption somewhere. |
Crosslink: http://stackoverflow.com/q/39852725/253468 Note: based on the SO question you could use #122, but you'd still have the memory issues. |
Hi, I have tried your code and it works. The images are decrypted and set using Glide perfectly. Thanks a lot for it. But there are some small improvements, that need to be done,
How can I tell Glide to use a different module specification for those full screen images? The Model would be same for both the cases, i.e.
Thanks again. :-) |
|
|
So, no, it's not encrypted. Having an encrypted cache needs you to replace some of decoder, encoder, and sourceEncoder, check out #735. I keep assuming that your original files are on local storage, so you could simply use NONE. |
Thanks. I had initially used NONE, but that is decrypting the thumbnails every time. The files are locally available, but it is not about just reading the files, they need to be decrypted as well. So, a cache is needed for better performance. |
Mind you that if you go ahead and enable Glide cache encryption (#735), you won't be in a much better position than using NONE. I'll close this now since the original issue is solved. |
I was also thinking of the same. Better I keep the cache unencrypted. Actually, there are some important documents of the users which I need to download and store locally. And as they are quite sensitive, I need to keep them encrypted. Now Glide solves the issue loading encrypted images very well. Thanks to you. :-) |
I'm in the same situation as the OP. (i.e: saving the encrypted image files locally) The major problem for me is that the encrypted image files are full screen sized, and I want to display them in a grid, so the decryption process from the original big image takes pretty heavy time to do for Glide. I could add decoder and encoder for the disk caching, but I can see that you still suggest to not save anything using the disk cache. I think that if I would use the disk cache to save the result image (default behavior), the decryption process from the disk cache would be much more efficient than encrypting it from the original big image file, as the disk cache has a "thumbnail" encrypted version (smaller in size), isn't it? Maybe I misunderstood your original statement, or the OP isn't really in the same situation as mine. |
I'm really looking for some help please :) |
I have been using Glide in several projects, and have always been a fan of it.
In my current project, I am implementing a small feature where I need to show some encrypted images in a RecyclerView. I would like to use Glide but I can not do something like this,
as I need to decrypt to images first and then set them.
I already have an AsyncTask that decrypts those images and gives me a Bitmap to set in an ImageView.
and here is the AsyncTask,
and the method to decrypt the thumbnail and get the Bitmap,
Everything works fine. The images are decrypted and set correctly. But if there are a lot of images, then the memory footprint shoot up high and there are several GCs resulting in a slightly laggy scrolling.
I would love to use Glide here as well. Is there a way I can use Glide to do this if I pass it the Bitmap or the CipherInputStream with the decrypted bytes? Can that be done using
ResourceDecoder
in Glide or I need something else?A detailed answer would be really helpful.
The text was updated successfully, but these errors were encountered: