答案 0 :(得分:0)
您可以通过调用Glide.get(context).register()方法将自定义ModelLoader类注册到Glide。在ModelLoader中,您可以通过实现 getResourceFetcher 方法告诉Glide如何从数据库加载图像资源,并返回自定义DataFetcher实例。
这是一个例子:
DBImageUri类:
int main(){
int testArray[] = {-2,0,44,12,-45,17,934,-21,67,88,91,1,0,6};
int n = 14;
int foundNegatives = countNegative(testArray,n);
if ( ! foundNegatives ) {
cout << "No Negative Numbers";
}
system("PAUSE");
//EXIT_SUCCESS;
return 0;
}
DBDataFetcher类:
public class DBImageUri {
private String uriString;
public DBImageUri(String uriString){
this.uriString = uriString;
}
@Override
public String toString(){
return uriString;
}
}
DBModelLoader类:
public class DBDataFetcher implements DataFetcher<InputStream> {
private DBImageUri uri;
private int width;
private int height;
private InputStream stream;
public DBDataFetcher(DBImageUri uri, int width, int height){
this.uri = uri;
this.width = width;
this.height = height;
}
@Override
public InputStream loadData(Priority priority){
String uriString = this.uri.toString();
stream = //**load image based on uri, and return InputStream for this image. this is where you do the actual image from database loading process**;
return stream;
}
@Override
public String getId(){
//width & height should be ignored if you return same image resources for any resolution (return uri.toString();)
return uri.toString() + "_" + width + "_" + height;
}
@Override
public void cleanup(){
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// Ignore
}
}
}
@Override
public void cancel(){
}
}
然后通过调用:
将ModelLoader添加到Glide注册表中public class DBModelLoader implements ModelLoader<DBImageUri, InputStream> {
@Override
public DataFetcher<InputStream> getResourceFetcher(DBImageUri model, int width, int height){
return new DBDataFetcher(model, width, height);
}
public static class Factory implements ModelLoaderFactory<DBImageUri, InputStream>{
@Override
public ModelLoader<DBImageUri, InputStream> build(Context context, GenericLoaderFactory factories){
return new DBModelLoader();
}
@Override
public void teardown(){
}
}
}
现在您可以加载数据库映像:
Glide.get(context).register(DBImageUri.class, InputStream.class, new DBModelLoader.Factory());