I am integrating html in android.I have created a web view, But i am not able load local html page. Surprisingly web view is loading 'http:google.com' properly, not my local html file. I have tried almost all possible link of SO. The error message is'Web Page could not be loaded'
WebView view = (WebView) findViewById(R.id.webView1);
view.loadUrl("file:///assets/www/trialhtml.html");![enter image description here][1]
Create your HTML file and place it in the assets folder, then you need to add these lines to the onCreate method like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/test.html");
setContentView(webView);
}
Here is the final result:
try below code :-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String myURL = "file:///android_asset/index.html";
WebView view = (WebView) findViewById(R.id.webView1);
/*By default Javascript is turned off,
* it can be enabled by this line.
*/
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new WebViewClient());
view.loadUrl(myURL);
}
Check ur file exist or not and also you can clean the project, and rebuild it.
paste your .html file in assets folder of your project folder. and create an xml file in layout folder with the fol code
WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
add fol code in activity
setContentView(R.layout.my);
WebView mWebView = null;
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/www/trialhtml.html"); //new.html is html file na
Related
I have a webview in my android app, the html file which gets loaded is on a server, however I need to address some files from app's assets folder. I wanted to ask how can I address those files.
Thanks
public class ViewWeb extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/aboutcertified.html"); // now it will not fail here
}
}
And in general you can use
getAssets().open("filename.txt"))
To get filea from assets
On a side note:please put some code next time that you have triedget
I have a Android Studio project, this project should have local HTML files.
I don't know if I understand the files structure, or the problem is another.
The file is not opened. Only display this error:
File Structure:
Code:
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webViewRevistas"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
public class revistas extends ActionBarActivity {
private WebView webViewRevistas;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_revistas);
WebView mWebView = (WebView) findViewById(R.id.webViewRevistas);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///assets/index.html");
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
The path is wrong.
It's not
file:///assets/...
but:
file:///android_asset/...
to fix this create a folder in "main" called "android_asset" and also create another folder called "assets" and place your html file in assets and call using this mWebView.loadUrl("file:///android_asset/YOUR HTML FILE.html");
I had the same problem and even though I dont call mWebView.loadUrl("file:///assets/YOUR HTML FILE.html"); it still some how works. which i find strange because thats where the html actually is!
You may have to duplicate your htmls for older android versions and place in android_asset also.
So again your folders should look like this main/android_assets/assets/YOUR HTML.html and main/assets/ and call with mWebView.loadUrl("file:///android_asset/YOUR HTML FILE.html");
Heres how my oncreate looks.
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_asset/index.html");
}
I am new in android development.I am developing an app,that can load a file path of .swf file from sd card, then want to play in webView. I don't want to use html file from android_asset folder. here is my code.this code show blank page.In this case what should i do?
`public class FlashFile extends Activity{
String sdCardFilePath;
private WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flash_view);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Bundle gotBusket = getIntent().getExtras();
sdCardFilePath = gotBusket.getString("key");
mWebView = new WebView(this);
mWebView = (WebView)findViewById(R.id.webView1);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.getSettings().setAllowFileAccess(true);
mWebView.loadUrl("file://"+sdCardFilePath);
}`
Trouble in loading local html file from asset into a webview. HTML page has javascript code, so I have used webview.setJavaScriptEnabled(true) to enable it. It loads the html page, but not all the widgets properly. Any suggestions...
public class ViewWeb extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
WebView wv;
wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("file:///android_asset/hello.html");
}
}
I want to load a picture called map.png set in my drawable resource in the WebView.
I found in another answer this suggestion webview.loadUrl("file://...") but I don't understand how to set it properly. I always get error that the requested file was not found. This is what I wrote
`public class Map extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
WebView mWebView =(WebView)findViewById(R.id.webMap);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file://res/drawable/map");`
This is the XML
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webMap"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
</WebView>
setContentView(R.layout.map);
WebView mWebView =(WebView)findViewById(R.id.webMap);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("file:///android_assets/map)");
This is a complete screen shot
This is the new code but I get an error
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
WebView myWebView = (WebView) findViewById(R.id.webMap);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setBuiltInZoomControls(true);
myWebView.setWebViewClient(new MyWebViewClient());
myWebView.loadUrl("file://mnt/sdcard/map.png");
You can access like this:-
file:///android_res/drawable/YOUR_FILE_NAME
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
Try with
mWebView.loadUrl("file://res/drawable/map.png")