Andrew
Andrew

Reputation: 21076

Size image to fit WebView dimensions

An API I hit returns a URL to an image. I want to use a WebView, feed it this URL and have it display the image. However, I want the WebView to be a static 90dip x 90dip (images are square). The images are bigger than 90px. Is there a way I can tell the WebView to size the image to its own dimensions?

Upvotes: 1

Views: 570

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

Have you tried it yet? Does it not work, if not what does it do instead?

I think you could use an ImageView to display the image with no problems. You can use a method like this to return to you a Drawable object from a url, which you can then set to the ImageView with setImageDrawable(img);

 /***********************************************************
 * This method will return the image from a URL.
 * Note: This should be called from the UI thread.
 ***********************************************************/
public Drawable getRemoteImage(final URL aURL) {
    try {
         final URLConnection conn = aURL.openConnection();
         conn.connect();
         final BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
         final Drawable d = Drawable.createFromStream(bis, "src");
         bis.close();
         return d;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

ImageView's I know for a fact you can set to a static size (90dip x 90dip) and it will handle scaling the image down if need be to make it fit in that size, WebView might try to make it scrollable or something, I am not sure.

Upvotes: 2

Related Questions