Android 使用 Picasso 获取带位图的回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20181491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Picasso to get a callback with a Bitmap
提问by Steve M
I'm using Picassoto download images for my app.
我正在使用毕加索为我的应用下载图像。
I'm in a situation where I need to access the Bitmap
first before it's loaded into the ImageView
. The presence of the Downloader.Response
class seems to suggest this is possible, but I can't find any use examples. I don't want to write a bunch more code to asynchronously handle this one particular case if it's possible to do with Picasso.
我处于一种情况,我需要在Bitmap
第一个加载到ImageView
. 该Downloader.Response
课程的存在似乎表明这是可能的,但我找不到任何使用示例。如果可以使用毕加索,我不想编写更多代码来异步处理这个特殊情况。
Can anyone show me how to do it?
谁能告诉我怎么做?
回答by Steve M
Found the answer on github in case anyone is wondering:
在 github 上找到答案以防万一有人想知道:
private Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
}
private void someMethod() {
Picasso.with(this).load("url").into(target);
}
@Override
public void onDestroy() { // could be in onPause or onStop
Picasso.with(this).cancelRequest(target);
super.onDestroy();
}
The post recommends not using an anonymous callback, and instead using an instance variable for target.
该帖子建议不要使用匿名回调,而是对目标使用实例变量。
回答by AbdulMomen ?????????
taken from here:
取自这里:
Picasso.with(this)
.load(url)
.into(new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
/* Save the bitmap or do something with it here */
//Set it in the ImageView
theView.setImageBitmap(bitmap);
}
});
Updated (May 04, 2016):
更新(2016 年 5 月 4 日):
Picasso.with(this)
.load(youUrl)
.into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
Updated (November 22, 2016)
更新(2016 年 11 月 22 日)
or using a strong reference for Target
so that it wont be garbage collected
或使用强引用Target
以便它不会被垃圾收集
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
void foo() {
Picasso.with(getContext()).load(getUrl()).into(target);
}
Kotlin
科特林
object: com.squareup.picasso.Target {
override fun onBitmapFailed(e: java.lang.Exception?, errorDrawable: Drawable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
}
}
回答by Oleg Tarashkevich
What can be easy than next:
什么比下一个更容易:
val url: String = "https://...."
val bitmap: Bitmap = Picasso.with(context).load(url).get()
Should be called from not the main thread!
应该从不是主线程调用!
or with RxJava 2:
或使用 RxJava 2:
fun getBitmapSingle(picasso: Picasso, url: String): Single<Bitmap> = Single.create {
try {
if (!it.isDisposed) {
val bitmap: Bitmap = picasso.load(url).get()
it.onSuccess(bitmap)
}
} catch (e: Throwable) {
it.onError(e)
}
}
Retrieve Bitmap:
检索位图:
getBitmapSingle(Picasso.with(context), "https:/...")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ bitmap ->
// val drawable = BitmapDrawable(context, bitmap)
}, Throwable::printStackTrace)
I used Picasso v.2.5.2
我使用了毕加索 v.2.5.2
回答by Sergey Aldoukhov
I thought maybe some of you would like an RxJava version of the above answer... Here it is:
我想也许你们中的一些人想要上述答案的 RxJava 版本......这里是:
public static Observable<Bitmap> loadBitmap(Picasso picasso, String imageUrl) {
return Observable.create(new Observable.OnSubscribe<Bitmap>() {
@Override
public void call(Subscriber<? super Bitmap> subscriber) {
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
subscriber.onNext(bitmap);
subscriber.onCompleted();
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
subscriber.onError(new Exception("failed to load " + imageUrl));
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
subscriber.add(new Subscription() {
private boolean unSubscribed;
@Override
public void unsubscribe() {
picasso.cancelRequest(target);
unSubscribed = true;
}
@Override
public boolean isUnsubscribed() {
return unSubscribed;
}
});
picasso.load(imageUrl).into(target);
}
});
}
P.S. When subscribing, store the subscription reference on your activity, otherwise, target will be GC'd before you receive a response...
PS 订阅时,将订阅引用存储在您的活动中,否则,在您收到响应之前,目标将被 GC 处理...
回答by saleh sereshki
with using target garbage collector run out your plans.so I do a trick with this sample code:
使用目标垃圾收集器耗尽了你的计划。所以我用这个示例代码做了一个技巧:
ImageView bitmapImageView = new ImageView(context);
picasso.with(context).load(url).into(bitmapImageView);
Bitmap bitmap = ((BitmapDrawable)bitmapImageView.getDrawable()).getBitmap();
now you can enjoy your bitmap; :))
现在您可以欣赏您的位图了;:))