I am trying to use Picasso from Square to pull jpg from a url and then add to an EditText. The reason for Picasso is that it is very easy to implement. As you can see, I am using the placeholder ImageView in which Picasso imports the image from the provided URL and then converts this ImageView to Drawable. The same goes for ImageGetter. However, when using the configuration below, I get a null pointer error. (Note that with simple extraction from application resources instead of the drawImage variable below, this configuration works, but I'm trying to extend it to pull resources from a URL).
Is something wrong here? Or a more efficient way to do this?
The way to add 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:
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;
}
};