1

I have a TextView which load a HTML content (which has img tags as well). My problem is that some images are too big so the app crashes. Is there any way to make the images to be all to the same size ?

A sample of my code :

            String EntireStire = "<b>" + mItem.title + " </b> <br> <img src='"
                + mItem.photo + "' > " + " <br><small>" + mItem.description
                + " <br> " + mItem.content + "</small>";

        noteView.setText(Html.fromHtml(EntireStire, new MyImageGetter(),
                null));
        noteView.setMovementMethod(LinkMovementMethod.getInstance());
    }

    return rootView;
}

private class MyImageGetter implements Html.ImageGetter {

    @Override
    public Drawable getDrawable(String arg0) {
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(
                    (InputStream) new URL(arg0).getContent(), null, null);
            @SuppressWarnings("deprecation")
            Drawable drawable = new BitmapDrawable(bitmap);
            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
                    drawable.getIntrinsicHeight());
            return drawable;
        } catch (Exception e) {
            Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
            return d;
        }
    }
}
Cosmin Rusu
  • 37
  • 1
  • 8
  • You can resize your bitmap like this: http://stackoverflow.com/a/4837803/2045570 – nedaRM Aug 14 '13 at 00:42
  • You can use some capped width or height to force all the images shown will be of same size. Check my answer below for details. – Khobaib Jan 04 '14 at 05:44

3 Answers3

2

Use this code instead, it resizes your image

private class MyImageGetter implements Html.ImageGetter {
    private final Context context;

    public MyImageGetter (Context context){
        this.context = context;
    }

    @Override
    public Drawable getDrawable(String source) {
        int path = context.getResources().getIdentifier(source, "drawable",
                BuildConfig.APPLICATION_ID);
            Drawable drawable = context.getResources().getDrawable(path);
            Display display = getWindowManager().getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            int widthD = size.x;
            int heightD = size.y;
            drawable.setBounds(0, 0, widthD, (widthD* drawable.getIntrinsicHeight()/ drawable.getIntrinsicWidth()));
            return drawable;

    }
}
Rune FS
  • 21,497
  • 7
  • 62
  • 96
Elaman Tun
  • 21
  • 2
1

Using a capped width/height when you set bounds will ensure that all the images will be of same size. Here, I modified your MyImageGetter method a bit.

private class MyImageGetter implements Html.ImageGetter {

@Override
public Drawable getDrawable(String arg0) {
    Bitmap bitmap;
    try {
        bitmap = BitmapFactory.decodeStream(
                (InputStream) new URL(arg0).getContent(), null, null);
        @SuppressWarnings("deprecation")
        Drawable drawable = new BitmapDrawable(bitmap);
        float multiplier = (float)SOME_CAPPED_WIDTH / (float)drawable.getIntrinsicWidth();
        int width = (int)(drawable.getIntrinsicWidth() * multiplier);
        int height = (int)(drawable.getIntrinsicHeight() * multiplier);
        drawable.setBounds(0, 0, width, height);
        return drawable;
    } catch (Exception e) {
        Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
        return d;
    }
}

}

Also to support multiple screen-size, you can use DisplayMetrics class.

Khobaib
  • 1,577
  • 3
  • 21
  • 29
-1
Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());

if you want to resize the height and width then you can change

Drawable d = getResources().getDrawable(R.drawable.ic_launcher);
        d.setBounds(0, 0, 100, 100);

or if you want to compress it do following

Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream out = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
drawable = new BitmapDrawable(_context.getResources(),bitmap);