Search code examples
androidwebviewimageviewscaledownload

Android WebView or ImageView?


I have an App that downloads and displays images from URL's. This works fine using

URLConnection conn = urls[i].openConnection();
conn.connect();
if(conn.getContentLength() > 0)
is = conn.getInputStream();
bis = new BufferedInputStream(is);
Bitmap temp = BitmapFactory.decodeStream(bis);

I noticed that WebView's download and display images much faster then the above code so I thought i would try.

String url = "http:\\image.jpg"
int mWidth = getSystemService(Context.WINDOW_SERVICE).getDefaultDisplay().getWidth()
webView.loadData("<html><body> <img src=\"" + url + "\" width=\"" + mWidth + "px\" height=\"auto\"></body></html>", "text/html", null);

Although this gets the image much faster it does not scale the image to the size of the screen, the large images always go beyond the WebView's width and require scrolling to view properly.

Does anyone know how i can fix the issue to display the image properly in the WebView or how to speed up the download of the image in the first place?

I would prefer to have the download speeded up, but i am happy to use the WebView if i can scale the image correctly.


Solution

  • Set the image elements width to 100% via CSS, height should scale according to the aspect/ratio of the image.