Munir
Munir

Reputation: 759

Html.imageGetter don't show img in Base64 android

I am trying to show a img in Base64 with Html.fromHtml

I have a tag with Base64 string of one image.

That is my code:

public class GlossaryAdapter extends BaseAdapter{

    ...
    private Resources res;

    public GlossaryAdapter(Context context, ...) {
        this.res = context.getResources();
        ...
    }

    public View getView(int position, View convertView, ViewGroup arg2) {
        ...
        holder.tvContent.setText(Html.fromHtml(glossary.getContent(), new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {            
                try {
                     byte[] data;
                     data = Base64.decode(source,Base64.DECODE);
                     Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);    
                     return new BitmapDrawable(res, bitmap);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }, null));

glossary.getContent() contains:

<img src="AAAY5671NF..." />

I tested this string in a html page and works. Show the image.

I am using Android 1.6. And this Base64 class: http://androidcodemonkey.blogspot.com/2010/03/how-to-base64-encode-decode-android.html

I got no errors. But nothing is showing. If I change the return to 'null' I got a small gray square.

any ideas?

Upvotes: 2

Views: 1302

Answers (1)

Munir
Munir

Reputation: 759

After a long time... get imageBytes(BLOB) from db. Enconde to Base64 and pass to src. Decode and set bounds of bitmap. Works!

@Override
    public Drawable getDrawable(String source) {            
            try {
                 byte[] data;
                 data = Base64.decode(source,Base64.DECODE);
                 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);    
                 Drawable d = new BitmapDrawable(res, bitmap);
                 d.setBounds(0,0,72,72)   // <-----
                 return d;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }

Upvotes: 3

Related Questions