使用 Picasso 将图像放入 Drawable [英] Use Picasso to Place Image Into Drawable
问题描述
我正在尝试使用 Square 的 Picasso 从 URL 中提取 jpg,然后附加到 EditText.Picasso 的原因是它在实现中非常轻量级.可以看出我正在使用占位符 ImageView,毕加索将从提供的 URL 导入图像,然后将该 ImageView 转换为 Drawable.ImageGetter 也是如此.但是,在使用以下配置时,我收到空指针错误.(请注意,当简单地使用应用程序资源中的 drawable 代替下面的drawImage"变量时,此配置有效,但我正在尝试扩展它以从 URL 中提取资源).
I am attempting to use Picasso from Square to pull a jpg from a URL and then append to an EditText. The reason for Picasso is that it's very lightweight in the implementation. As can be seen I am using a placeholder ImageView, whereby Picasso will import the image from the URL provided, and then I convert that ImageView into a Drawable. The same goes for the ImageGetter. However I receive null pointer error when using the configuration below. (Note when simply using a drawable from the application resources in place of the 'drawImage' variable below, this configuration works, but I'm trying to extend it to pull resources from a URL).
这里有什么不对的地方吗?或者更有效的方法来做到这一点?
Is there something out of place here? Or a more efficient way to do this?
附加到 EditText 的方法:
Method to append to EditText:
public void appendToMessageHistory(String username, String message) {
if (username != null && message != null) {
ImageView image = new ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load("http://localhost:3000/uploads/campaign/image/2/2.jpg").into(image);
Drawable drawImage = image.getDrawable();
messageHistoryText.append(Html.fromHtml("<b>" + username + ":"
+ "</b>" + "<br>"));
messageHistoryText.append(Html.fromHtml(message + "<hr>" + "<br>")
+ System.getProperty("line.separator") + "");
messageHistoryText.append(Html.fromHtml("<img src = '"
+ drawImage + "'/>",
imageGetter, null));
}
}
ImageGetter:
The ImageGetter:
ImageGetter imageGetter = new ImageGetter() {
@Override
public Drawable getDrawable(String source) {
ImageView image = new ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load("http://localhost:3000/uploads/campaign/image/2/2.jpg").into(image);
Drawable drawImage = image.getDrawable();
drawImage.setBounds(0, 0, drawImage.getIntrinsicHeight(), drawImage.getIntrinsicWidth());
return drawImage;
}
};
推荐答案
您对毕加索有些怀念.您可以在 inTo 方法中设置一个匿名 Target 类,并将位图设置为您拥有的任何对象:
There is something you miss about Picasso. You can set an annonymous Target class in inTo method like and set bitmap to any object you have :
Picasso.with(getBaseContext()).load("your url").into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
// TODO Create your drawable from bitmap and append where you like.
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
});
编辑所以这就是你这样做的方式:
EDIT so this is how you do that:
public void appendToMessageHistory(String username, String message) {
if (username != null && message != null) {
ImageView image = new ImageView(getApplicationContext());
Picasso.with(getBaseContext()).load("image url").into(new Target() {
@Override
public void onPrepareLoad(Drawable arg0) {
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1) {
Drawable drawImage = new BitmapDrawable(getBaseContext().getResources(),bitmap);
messageHistoryText.append(Html.fromHtml("<b>" + username + ":"
+ "</b>" + "<br>"));
messageHistoryText.append(Html.fromHtml(message + "<hr>" + "<br>")
+ System.getProperty("line.separator") + "");
messageHistoryText.append(Html.fromHtml("<img src = '"
+ drawImage + "'/>",
imageGetter, null));
}
@Override
public void onBitmapFailed(Drawable arg0) {
}
});
}
}
这篇关于使用 Picasso 将图像放入 Drawable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!