I would like to store my images in an sqlite db, in blob-s, and maybe encrypt them. Is it possible to use the Android-Universal-Image-Loader with images from an sqlite database?
Can Universal image loader for android work with images from sqlite db?
Asked Answered
Did you tried using it? –
Discriminate
I'm using it now to just load images from the file system. I did not see any methods in the docs or the source that would accept a byte[] as input. When I load the bitmap from the db, that would return a byte [].. –
Neodarwinism
UIL doesn't support of images from SQLite DB out of the box. But you can add this support yourself, you just need come up with new scheme/protocol name (e.g. db://), implement own ImageDownloader
and set it to configuration.
For example:
Lets choose own scheme db
so our URIs will look like "db://...".
Then implement ImageDownloader
. We should catch URIs with our scheme, parse it, find needed data in DB and create InputStream
for it (it can be ByteArrayInputStream
).
public class SqliteImageDownloader extends BaseImageDownloader {
private static final String SCHEME_DB = "db";
private static final String DB_URI_PREFIX = SCHEME_DB + "://";
public SqliteImageDownloader(Context context) {
super(context);
}
@Override
protected InputStream getStreamFromOtherSource(String imageUri, Object extra) throws IOException {
if (imageUri.startsWith(DB_URI_PREFIX)) {
String path = imageUri.substring(DB_URI_PREFIX.length());
// Your logic to retreive needed data from DB
byte[] imageData = ...;
return new ByteArrayInputStream(imageData);
} else {
return super.getStreamFromOtherSource(imageUri, extra);
}
}
}
Then we set this ImageLoader
to configuration:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
...
.imageDownloader(new SqliteImageDownloader(context))
.build();
ImageLoader.getInstance().init(config);
And then we can do following to display image from DB:
imageLoader.displayImage("db://mytable/13", imageView);
Ok, I just did that, and it works great on weaker phones, for example the galaxy ace, it takes around 20ms to load a 10K image from db. But when I run the same code on the nexus 4 or galaxy s3, it takes around 1-3 SECONDS to load the same 10k image :) any ideas why this would be the case? –
Neodarwinism
To calrify, my code is the same as the one you pasted in, except that one line that needed to be replaced: byte[] imageData = db.getImageData(imageId); The db read takes around 4-10ms on the nexus 4, but displaying the image takes 1-3 seconds. Other than the imagedownloader settings, I'm using the default settings everywhere. –
Neodarwinism
Ok I figured it out..I should not be opening and closing the database all the time, and just leave it open while it is in use, so your code works fine! Thanks! –
Neodarwinism
@Neodarwinism When u are closing the database? I am opening database in the
SqliteImageDownloader
constructor. –
Maudmaude © 2022 - 2024 — McMap. All rights reserved.