How to implement Html.IImageGetter interface?

duguangzengduguangzeng CNMember ✭✭
edited September 2013 in Xamarin.Android

I am developing an app for Android using mono for android. And I use Textview to display html code incluing text and pics, so I have to create a new class that implement Html.IImageGetter interface. Unfortunatly I have no idea about how to implement the interface, especially the "public IntPtr Handle" part. please help me out.Here is the draft code.

public class NewsImageGetter:Html.IImageGetter
{

    public Drawable GetDrawable(string source)
    {
        Drawable drawable;
        try
        {
            var url = new Java.Net.URL(source);
            drawable = Drawable.CreateFromStream(url.OpenStream(), null);
        }
        catch (Exception)
        {

            return null;
        }
        drawable.SetBounds(0, 0, drawable.IntrinsicWidth, drawable.IntrinsicHeight);
        return drawable;
    }

    public IntPtr Handle
    {
        get { throw new NotImplementedException(); }
        //I don't know what to write here. Help.
    }

    public void Dispose()
    {
        throw new NotImplementedException();
    }
}

Best Answers

Answers

  • duguangzengduguangzeng CNMember ✭✭
    edited September 2013

    thanks, Cheesebaron. I do what you told me, and problem has been solved. thank you so much.

  • duguangzengduguangzeng CNMember ✭✭

    Here is my final code, and it works well. I paste it out, in case someone else needs it.

    public class NewsImageGetter:Java.Lang.Object, Html.IImageGetter

    {

        public Drawable GetDrawable(string source)
        {
            Drawable drawable;
            Bitmap bitMap;
            BitmapFactory.Options bitMapOption;
            try
            {
                 bitMapOption = new BitmapFactory.Options();
                bitMapOption.InJustDecodeBounds = false;
                bitMapOption.InPreferredConfig = Bitmap.Config.Argb4444;
                bitMapOption.InPurgeable = true;
                bitMapOption.InInputShareable = true;
                var url = new Java.Net.URL(source);
    
                bitMap = BitmapFactory.DecodeStream(url.OpenStream(),null,bitMapOption);
                drawable = new BitmapDrawable(bitMap);
    
            }
            catch (Exception)
            {
                return null;
            }
    
            drawable.SetBounds(0, 0,bitMapOption.OutWidth, bitMapOption.OutHeight);
            return drawable;
    
        }
    
        public new IntPtr Handle
        {
            get { return  base.Handle; }
        }
    
        public new void Dispose()
        {
            base.Dispose();
        }
    }
    
  • SacomaniSacomani USMember ✭✭

    but does work on gmail.... my html images are not showing on email app

  • SagarPanwalaSagarPanwala USMember ✭✭✭

    @Cheesebaron
    How to use this class?

  • huydangthaihuydangthai Member ✭✭

    how do you use it to display text and img ?

Sign In or Register to comment.