我想将图像存储在blob-s中的sqlite db中,并可能对其进行加密。
是否可以将Android-Universal-Image-Loader与来自sqlite数据库的图像一起使用?

最佳答案

UIL不支持开箱即用的SQLite DB中的图像。但是您可以自己添加此支持,您只需要提供新的方案/协议(protocol)名称(例如 db:// ),实现自己的ImageDownloader并将其设置为配置即可。

例如:

让我们选择自己的方案db,以便我们的URI看起来像“db:// ...”

然后实现ImageDownloader。我们应该使用我们的方案捕获URI,对其进行解析,在DB中找到所需的数据并为其创建InputStream(可以是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);
        }
    }
}

然后我们将此ImageLoader设置为配置:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
        ...
        .imageDownloader(new SqliteImageDownloader(context))
        .build();

ImageLoader.getInstance().init(config);

然后,我们可以执行以下操作来显示来自DB的图像:
imageLoader.displayImage("db://mytable/13", imageView);

07-28 01:12
查看更多