Using Picasso to Place an Image in a Drawing

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;


}

};

+4
1

-, . inTo, , , :

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


                }
            });

:

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


                    }
                });




        }
    }
+12

All Articles