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?
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));
}
}
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;
}
};
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…