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?
-
Did you tried using it?– userApr 26, 2013 at 8:53
-
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 []..– TamasApr 26, 2013 at 9:11
1 Answer
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?– TamasApr 29, 2013 at 8:03
-
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.– TamasApr 29, 2013 at 8:41
-
1Ok 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!– TamasApr 29, 2013 at 9:53
-
@Tamas When u are closing the database? I am opening database in the
SqliteImageDownloader
constructor.– DoryNov 11, 2014 at 10:50