如果你关注Glide示例的用法,它会得到那个得到()方法属于java.util.concurrent.Future对象。未来的班级定义由官方文档给出如下。
public interface Future<V>
Future表示异步计算的结果 。提供的方法用于检查计算是否完成,是否等待完成,以及检索计算结果 。只有在计算完成时才能使用 方法获取结果,如果需要,可以将其阻塞 ,直到准备就绪。取消是通过取消方法执行的。 提供了其他方法来确定任务是否正常完成或取消了 。一旦计算完成,计算不能被取消。如果您想为可取消性使用Future而不提供可用结果,则您可以通过 声明Future形式的类型,并作为 基础任务的结果返回null。
用法示例(注意,下列各类都是编造的。)
interface ArchiveSearcher { String search(String target); }
class App {
ExecutorService executor = ...
ArchiveSearcher searcher = ...
void showSearch(final String target)
throws InterruptedException {
Future<String> future
= executor.submit(new Callable<String>() {
public String call() {
return searcher.search(target);
}});
displayOtherThings(); // do other things while searching
try {
displayText(future.get()); // use future
} catch (ExecutionException ex) { cleanup(); return; }
}
}
让我们看看会一步发生了一步:
Bitmap theBitmap = Glide.
with(this). //in Glide class and returns RequestManager
load(image_url). // in RequestManager and returns RequestBuilder<Drawable>
asBitmap(). //in RequestBuilder and returns RequestBuilder<Bitmap>
submit(). // in RequestBuilder and returns FutureTarget<TranscodeType> which extends Future<>
get(); // this belongs to Future object which is the result of async computation
public static RequestManager with(Context context) {
return getRetriever(context).get(context);
}
public RequestBuilder<Drawable> load(@Nullable Object model) {
return asDrawable().load(model);
}
public RequestBuilder<Bitmap> asBitmap() {
return as(Bitmap.class).transition(new GenericTransitionOptions<Bitmap>())
.apply(DECODE_TYPE_BITMAP);
}
public FutureTarget<TranscodeType> submit() {
return submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}
public interface FutureTarget<R> extends Future<R>, Target<R> {
}
但更恰当和安全解决方法是使用回调
Glide
.with(this)
.load(image_url)
.asBitmap()
.into(new SimpleTarget<Bitmap>(100,100) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
//resource is the resulting bitmap
}
});
查看[Glide文档](https://github.com/bumptech/glide/wiki)。看起来你应该使用'into()'而不是'get()'。 – rundavidrun
它看起来像你缺少添加asBitmap()看到这个答案https://stackoverflow.com/a/27394484/7134908 –