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;
}
};
Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details. In your case you can parse every image URL and use RecyclerView to show them along with Picasso.
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) {
}
});
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With