Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

android - 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;


}

};

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


                    }
                });




        }
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...