0

I was curious if it was possible to add an image drawable already in memory to a WebView in Android.

Currently I am being passed a Drawable object and would like to add it directly to the WebView without saving it to disk.

Is this possible?

Example:

WebView myWebView = (WebView) findViewById(R.main.Content);
InputStream is = (InputStream) new URL(imageURL).getContent();
Drawable image = Drawable.createFromStream(is, "src");
myWebView.add(image); //Fictitious code

I have tried using the WebView's "SetBackground" method, however I would not like to limit my min API to Jellybean, as well as I was only presented with a blank screen.

Thanks

WeldFire
  • 194
  • 1
  • 3
  • 8

2 Answers2

1

Simple answer to your question, This is not possible to add the image from the application resources or any downloaded image directly in the WebView directly which you are trying.

For this all you need to add in your website and load in the WebView or If you have image link then load that link in the WebView.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • My image is not in application resources. It is a drawable already in memory. – WeldFire Feb 15 '14 at 19:08
  • It is possible to add an image from the applications resources to the WebView by using a `file:///android_asset/` URL. See: http://stackoverflow.com/questions/19857225/android-webview-loadurlfile-android-asset-index-htmlhome-failed – marcin.kosiba Feb 17 '14 at 10:35
  • @marcin.kosiba, No you can not direct load the drawable from the application resources in the webview, I know about the link which you shared that you will have to add the html website page and then use the ` – Ajay S Feb 17 '14 at 14:01
  • 1
    @TGMCians - agreed. What I'm pointing out is the way you stated your answer which makes it seem as if you can't load images coming from application resources *at all*. – marcin.kosiba Feb 17 '14 at 14:03
  • I wrote same in the answer - For this all you need to add in your website and load in the WebView or If you have image link then load that link in the WebView – Ajay S Feb 17 '14 at 14:07
1

The WebView does it's own rendering and doesn't have a hook for you to drop in a decompressed bitmap.

If you simply want to avoid writing to disk, then you could use WebViewClient.shouldInterceptRequest to return the InputStream for the image to the WebView (something along the lines of this: https://stackoverflow.com/a/13142705/2977376).

If you want to use the Drawable directly then the only thing I can think of is to have a "hole" in your HTML (essentially an empty div sized to the size of the image) and then draw that image on top of the WebView yourself. This might be challenging since you'd need to handle scale, translate and clipping in your own code, so I wouldn't recommend it.

Community
  • 1
  • 1
marcin.kosiba
  • 3,221
  • 14
  • 19