views:

53

answers:

1

Hi all,

I want to send down a webpage from a server to WebView components on Android handsets. I've already learned how to enable the webpage to talk to the JavaScript handler, so the native application can interact with the webpage. I am, however, stuck on images.

I want the webpage that comes from a server to somehow tell the app which image (stored in either res or in assets) to load. This way I don't have to send the image over the wire as well. Is this doable? It will make the process of loading WebView pages so much faster for my purposes.

Thanks!

A: 

You can fetch the http response from the remote url, parse it and replace the remote image urls with local ones via string replace. Then use this html code to display in the webview.

I want to send down a webpage from a server to WebView

Are you talking about 'push' rather than 'pull' mechanism (like as usual with loadUrl())? That's only available in 2.2

(Question: I guess working entirely with local web pages on the device doesn't work in your case, since you need updated versions from the server, right? You only know that the images won't change, correct?)

Sample code for fetching a remote html page - afterwards you'd do the string replacement for your image urls:

/**
 * Downloads a remote file and stores it locally
 * @param from Remote URL of the file to download
 * @param to Local path where to store the file
 * @throws Exception Read/write exception
 */
static private void downloadFile(String from, String to) throws Exception {
    HttpURLConnection conn = (HttpURLConnection)new URL(from).openConnection();
    conn.setConnectTimeout(15000); // timeout 15 secs
    conn.setDoInput(true);
    conn.connect();
    InputStream input = conn.getInputStream();
    FileOutputStream fOut = new FileOutputStream(to);
    int byteCount = 0;
    byte[] buffer = new byte[4096];
    int bytesRead = -1;
    while ((bytesRead = input.read(buffer)) != -1) {
        fOut.write(buffer, 0, bytesRead);
        byteCount += bytesRead;
    }
    fOut.flush();
    fOut.close();
}

Alternatively you could use

HttpClient httpClient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://www.myurl.com");
HttpResponse res = httpClient.execute(get);
if(res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
    InputStream input = res.getEntity().getContent();
    byte data[] = new byte[14];
    input.read(data);
    ....
Mathias Lin
Thanks for the response. Sorry, my question wasn't as clear as it should have been. This does help though, but left me asking: how do I reference an image in res or assets by url? It seems like this has the answer to that though: http://stackoverflow.com/questions/1747072/webview-not-loading-assets-correctly
js01
For reference, I don't want to "pull down". I want to simply loadUrl on demand, where the html returned from the server has direct references to image files stored in the .apk.
js01
Yep, you already found a link with how to acccess the assets, it's correct.
Mathias Lin