Loading an Android Resource into a Webview

Load resources image to webview

Try to append your URL with

String html = "<html><head></head><body><img src=\""+ URL + "\"></body></html>";
webView.loadDataWithBaseURL("", html, "text/html","utf-8", "");

Also Refer this link https://stackoverflow.com/a/14405801/1208563

Loading an Android Resource into a WebView

from this site

Using the resource id, the format is:

"android.resource://[package]/[res id]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/" + R.raw.myvideo);

or, using the resource subdirectory (type) and resource name (filename without extension), the format is:

"android.resource://[package]/[res type]/[res name]"

Uri path = Uri.parse("android.resource://com.androidbook.samplevideo/raw/myvideo");

how to load a picture from my resource in webview?

you can not access Drawable from application's drawable directory like

   mWebView.loadUrl("file://res/drawable/map");

for file from your asset directory of your app package you should use

   mWebView.loadUrl("content://"+Context.getResources().getAsset()+"filename");

EDIT: ok put your image in Applications asset directory, then for example write line,

  myWebView.loadUrl("file:///android_asset/testimage.jpg"); 

or use

    webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.loadUrl("file://mnt/sdcard/map.png");

try it

Get loaded resource files list from android webview

You can get the list of resources the WebView is trying to load by overriding WebViewClient.shouldInterceptRequest like so:

class MyWebViewClient extends WebViewClient {

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
android.util.Log.i("MyWebViewClient", "attempting to load resource: " + url);
return null;
}

@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
android.util.Log.i("MyWebViewClient", "attempting to load resource: " + request.getUrl());
return null;
}

}

Remember that shouldInterceptRequest is called on a background thread and so you need synchronize access to any shared data structures.

There is no Java API to find out whether the WebView has successfully loaded a given resource though.

Android WebView load html file from resource raw folder

Load the raw resource via a stream (to string):

using (var stream = Resources.OpenRawResource(Resource.Raw.someHTMLBasedFile))
using (var streamReader = new StreamReader(stream))
{
webView.LoadDataWithBaseURL("file:///android_asset/", streamReader.ReadToEnd(), "text/html", "UTF-8", "");
}

Note: Since you are loading via a resource, you can provide res/raw-<qualifiers> folders if you need to internationalize via the device's locale.

Can you load a local html resource into a WebView?

You absolute can, you just need to create a folder in an assets directory on the root of your project and you can have the browser load a url like this...

browser.loadUrl("file:///android_asset/UI/mylocalhtmlfile.htm");

Where UI in the above path is a folder under the assets folder.

Loading html file from local folder into webview

You can use:


WebView webView = // ...

webView.loadUrl("file:///myPath/myFile.html");

In an Android application, files can be read from 3 types of locations:

  • Internal storage: Each app has its own, file names are relative to this location. URL takes form file:///myFolder/myFile.html

  • External storage: Needs permission and may not always be available. Get the root folder by calling Environment.getExternalStorageDirectory(). So, construct the URL using: String url = "file:///" + Environment.getExternalStorageDirectory().toString() + File.separator + "myFolder/myFile.html"

  • Assets: Stored in the apk. Read-only access. URL takes form file:///android_asset/myFolder/myFile.html (See also Loading an Android Resource into a WebView)

Loading existing .html file with android WebView

ok, that was my very stupid mistake. I post the answer here just in case someone has the same problem.

The correct path for files stored in assets folder is file:///android_asset/* (with no "s" for assets folder which i was always thinking it must have a "s").

And, mWebView.loadUrl("file:///android_asset/myfile.html"); works under all API levels.

I still not figure out why mWebView.loadUrl("file:///android_res/raw/myfile.html"); works only on API level 8. But it doesn't matter now.



Related Topics



Leave a reply



Submit