6.根据不同的加载路径,选择合适的RequestHandler来创建BitmapHunter
static BitmapHunter forRequest(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats, Action action) {
Request request = action.getRequest();
//Picasso中默认保存了一个集合,里面存储了每一类图片的加载,判断和处理逻辑 ,比如网络图片,资源图片,硬盘图片,照片等
List<RequestHandler> requestHandlers = picasso.getRequestHandlers();
// Index-based loop to avoid allocating an iterator.
//noinspection ForLoopReplaceableByForEach
for (int i = 0, count = requestHandlers.size(); i < count; i++) {
RequestHandler requestHandler = requestHandlers.get(i);
//它会通过request中url的scheme来判断该使用哪一个RequestHandler
if (requestHandler.canHandleRequest(request)) {
return new BitmapHunter(picasso, dispatcher, cache, stats, action, requestHandler);
}
}
return new BitmapHunter(picasso, dispatcher, cache, stats, action, ERRORING_HANDLER);}
7. BitmapHunter被提交到线程池之后,接下来就该run方法执行了
@Override
public void run() {
try {
updateThreadName(data);
...
}
//重点!!!
result = hunt();
//得到bitmap后,执行Dispater中的方法
if (result == null) {
dispatcher.dispatchFailed(this);
} else {
dispatcher.dispatchComplete(this);
}
} catch (Downloader.ResponseException e) {
if (!e.localCacheOnly || e.responseCode != 504) {
...
}
dispatcher.dispatchFailed(this);
} catch (IOException e) {//重试
...
dispatcher.dispatchRetry(this);
} catch (OutOfMemoryError e) {//OOM异常的处理
StringWriter writer = new StringWriter();
stats.createSnapshot().dump(new PrintWriter(writer));
exception = new RuntimeException(writer.toString(), e);
dispatcher.dispatchFailed(this);
} catch (Exception e) {
...
dispatcher.dispatchFailed(this);
} finally {
...
}
} |