Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Picasso to Place Image Into Drawable

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;


}

};

like image 298
Sauron Avatar asked Aug 06 '14 03:08

Sauron


People also ask

How do you load an image into an ImageView from an image URL using Picasso?

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.


1 Answers

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) {


                    }
                });




        }
    }
like image 133
Ercan Avatar answered Oct 04 '22 19:10

Ercan