Using Picasso with Image Getter

picasso get error
picasso get not working
android textview and image loading from url
android-textview settext in html.fromhtml to display image and text
android webview load image from url
show image in textview android
html.imagegetter android example
android image tag

I ma building a chat application and I am attempting to append an image to an EditText, through use of Picasso to get the image from a URL and the append and ImageGetter to attach the image to the EditText. However, what I have implemented below does not work, as appending messages when using the app displays nothing (but the message does show up in the database).

I have tested without using Picasso, as simply just using the ImageGetter with an image resource within the app works just fine, only it's not from a URL as is required.

What is the proper way to configure the ImageGetter and/or the append method so that this functionality will work with Picasso? Or is there a simpler way?

Append method:

public void appendToMessageHistory(final String username,
            final String message) {
        if (username != null && message != null) {

            Picasso.with(getBaseContext())
                    .load("http://localhost:3000/uploads/campaign/image/2/2.jpg")
                    .into(new Target() {

                        @Override
                        public void onPrepareLoad(Drawable arg0) {

                        }

                        @Override
                        public void onBitmapLoaded(Bitmap bitmap,
                                LoadedFrom arg1) {
                            Drawable drawImage = new BitmapDrawable(
                                    getBaseContext().getResources(), bitmap);

                            drawImage.setBounds(0, 0,
                                    drawImage.getIntrinsicHeight(),
                                    drawImage.getIntrinsicWidth());
                            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) {

                        }
                    });

        }
    }

ImageGetter:

ImageGetter imageGetter = new ImageGetter() {
        Drawable imageUsed=null;

        @Override
        public Drawable getDrawable(String source) {

            Picasso.with(getBaseContext())
                    .load("http://localhost:3000/uploads/campaign/image/2/2.jpg")
                    .into(new Target() {

                        @Override
                        public void onPrepareLoad(Drawable arg0) {

                        }

                        @Override
                        public void onBitmapLoaded(Bitmap bitmap,
                                LoadedFrom arg1) {
                            Drawable drawImage = new BitmapDrawable(
                                    getBaseContext().getResources(), bitmap);

                            drawImage.setBounds(0, 0,
                                    drawImage.getIntrinsicHeight(),
                                    drawImage.getIntrinsicWidth());

                            imageUsed=drawImage;

                        }

                        @Override
                        public void onBitmapFailed(Drawable arg0) {

                        }
                    });

            return imageUsed;
        }

    };

Android, android documentation: Using Picasso as ImageGetter for Html.fromHtml. public class PicassoImageGetter implements Html.ImageGetter { private TextView� Using Picasso with Image Getter. advertisements. I ma building a chat application and I am attempting to append an image to an EditText, through use of Picasso to get the image from a URL and the append and ImageGetter to attach the image to the EditText.


I built upon Thomas' answer. I used the existing Picasso methods to download any images on a different thread and accounted for the placeholder Drawables as well.

public class PicassoImageGetter implements Html.ImageGetter {
    private TextView textView = null;

    public PicassoImageGetter() {}

    public PicassoImageGetter(TextView target) {
        textView = target;
    }

    @Override
    public Drawable getDrawable(String source) {
        BitmapDrawablePlaceHolder drawable = new BitmapDrawablePlaceHolder();

        Context context = FeedSurferApp.getContext();
        FeedSurferApp
                .getPicasso()
                .load(source)
                .error(ResourcesCompat.getDrawable(context.getResources(), R.drawable.connection_error, context.getTheme()))
                .into(drawable);

        return drawable;
    }

    private class BitmapDrawablePlaceHolder extends BitmapDrawable implements Target {
        protected Drawable drawable;

        @Override
        public void draw(final Canvas canvas) {
            if (drawable != null) {
                drawable.draw(canvas);
            }
        }

        public void setDrawable(Drawable drawable) {
            this.drawable = drawable;
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
            if (textView != null) {
                textView.setText(textView.getText());
            }
        }

        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            setDrawable(new BitmapDrawable(FeedSurferApp.getContext().getResources(), bitmap));
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {
            setDrawable(errorDrawable);
        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {}
    }
}

Picasso, I have tested without using Picasso, as simply just using the ImageGetter with an image resource within the app works just fine, only it's not from a URL as is� Using Picasso as ImageGetter for Html.fromHtml. Ping ICMP. Port Mapping using Cling library in Android. PorterDuff Mode. ProgressBar. ProGuard - Obfuscating and Shrinking your code. Project SDK versions. Publish .aar file to Apache Archiva with Gradle. Publish a library to Maven Repositories.


I found a way to use ImageGetter with Picasso Target:

    public Drawable getDrawable(String source) {

            final BitmapDrawablePlaceHolder result = new BitmapDrawablePlaceHolder();

            Picasso.with(getContext()).load(source).into(new Target() {
                @Override public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    final BitmapDrawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);

                    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());


                    result.setDrawable(drawable);
                    result.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

                    content.setText(content.getText());




                    // cache is now warmed up
                }
                @Override public void onBitmapFailed(Drawable errorDrawable) { }
                @Override public void onPrepareLoad(Drawable placeHolderDrawable) { }
            });
            return result;

}

This uses the cache and is no longer a synchronous call requiring the AsyncTask.

Credit to: Fetch images with Callback in Picasso?

Using Picasso with Image Getter, PicassoImageGetter.java. public class PicassoImageGetter implements Html. ImageGetter {. private AppTextView textView = null;. public PicassoImageGetter() {. }. Loads images for Bypass using Picasso. Contribute to Commit451/BypassPicassoImageGetter development by creating an account on GitHub.


Another simple way to use Picasso is to create a custom TextView and make it implement Target interface of Picasso. Then you can do something as simple as this.

        Picasso.get().load("Some_img_url").placeholder(R.drawable.ic_launcher_foreground).into(tv)

Below is the class

class TextViewWithImageLoader @JvmOverloads constructor(

context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr), Target {
enum class Direction { START, END, TOP, BOTTOM }

private var mWidthInPixels: Int = 0
private var mHeightInPixels: Int = 0
private var mDirection: Direction = Direction.START //default value

// This method initialize the required parameters for the TextView to load the image
fun setupImageLoader(widthInPixels: Int, heightInPixels: Int, direction: Direction) {
    mWidthInPixels = widthInPixels
    mHeightInPixels = heightInPixels
    mDirection = direction
}

// sets the size of the drawable
private fun setDrawableBounds(drawable: Drawable) {
    drawable.setBounds(0, 0, mWidthInPixels, mHeightInPixels)
}

// Sets the initial placeholder drawable
override fun onPrepareLoad(placeHolderDrawable: Drawable?) {
    //checking if height and width are valid
    if (placeHolderDrawable != null && mWidthInPixels > 0 && mHeightInPixels > 0) {
        setDrawableBounds(placeHolderDrawable)
        setDrawable(placeHolderDrawable)
    }
}

// set the drawable based on the Direction enum
private fun setDrawable(placeHolderDrawable: Drawable?) {
    when (mDirection) {
        Direction.START -> setCompoundDrawables(placeHolderDrawable, null, null, null);
        Direction.END -> setCompoundDrawables(null, null, placeHolderDrawable, null);
        Direction.TOP -> setCompoundDrawables(null, placeHolderDrawable, null, null);
        Direction.BOTTOM -> setCompoundDrawables(null, null, null, placeHolderDrawable);
    }
}

//In this method we receive the image from the url
override fun onBitmapLoaded(bitmap: Bitmap?, from: Picasso.LoadedFrom?) {
    //checking if height and width are valid
    if (mWidthInPixels > 0 && mHeightInPixels > 0) {
        val drawable = BitmapDrawable(resources, bitmap)
        setDrawableBounds(drawable)
        setDrawable(drawable)
    }
}

override fun onBitmapFailed(e: Exception?, errorDrawable: Drawable?) {
    //Do nothing as we are already setting a default value in onPrepareLoad method
    // you can add your logic here if required
 }
}

You can use this class directly or if you currently have your own custom view then just extend this class.

You can either go through this medium article for more details

OR

You can see the sample project on github

ImageGetter used for loading remote urls from <img> tag when , Essentially speaking, ImageGetter is a function interface used to wrap The solution is to download the image using Picasso while the result is� In the last blog posts, you've learned how to load images from various sources and how to use different kind of placeholders. This week's blog post is important if you cannot influence the size of the image to be loaded: resizing and scaling! Picasso Series Overview


A Failed Attempt to Implement Html.ImageGetter with Picasso , Complex image transformations with minimal memory use. Automatic memory and disk caching. Sample application screenshot. Features. Adapter Downloads. Resizing with Picasso. We can resize an image with respect to the aspect ratio using resize and specifying 0 for the other dimension as outlined here: // Resize to the width specified maintaining aspect ratio Picasso. with (this). load (imageUrl). resize (someWidth, 0). into (imageView);


Picasso, html = (Spannable) Html.fromHtml(content, imageGetter, null); Since I use Picasso to load remote images, I tried checking stackoverflow for� Picasso loads the image in two ways synchronously and Asynchronously..fetch () – It will asynchronously load the image into a background thread. This method only saves the image into the disk or memory cache. It’s neither going to load the image into image view nor it is going to return any Bitmap.


Android TextView and Image loading from URL — Part 1, Picasso is a popular open-source Android library for loading both local and Learn how to easily use it for handling your image loading needs. 1. Specify the {@link Downloader} that will be used for downloading images. Here comes the magic line. We load image from url into ImageView by using the code. Picasso.with (context).load (android_versions.get (i).getAndroid_image_url ()).resize ( 120, 60 ).into (viewHolder.img_android); This single line of code simplifies all your complex tasks. The img_android is the ImageView object.