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");
}
Related
Using webView.loadUrl is working for https site , but not with my local page which is located in /Applications/MAMP/htdocs/test.html
webView.loadUrl("/Applications/MAMP/htdocs/test.html") is not working
webView.loadUrl("http://localhost:8080/test.html"); is not working
Below is the test.html file
<!DOCTYPE html>
<html>
<head>
<title>Sample Page</title>
</head>
<body>
<h2>JS alert Example</h2>
<button onclick="myFunction()" >Try it</button>
<script>
function myFunction (){
Android.showToast("Sample Android toast message");
}
</script>
</body>
</html>
Now I want to load this page using webView .
Below is the mainactivity.java code
public class MainActivity extends AppCompatActivity {
private WebView webView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
webView = (WebView) findViewById(R.id.webview);
webView.loadUrl("http://127.0.0.1:8080/test.html");
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
webView.addJavascriptInterface(new WebAppInterface (this), "Android");
}
}
Below is my WebInterface java class :
public class WebAppInterface {
private Context context;
public WebAppInterface (Context context){
this.context = context;
}
#JavascriptInterface
public void showToast (String message){
Toast.makeText(context,message,Toast.LENGTH_LONG).show();
}
}
Now ,I cant understand why it is not loading the web page mentioned in webView.loadUrl . Note: I having given internet permissions in manifest file.
So if the file is local in the device (android phone), you need to have the path of the file. If the file is bundled in the assets, you can open the file like:
webView.loadUrl("file:///android_asset/filename.html");
Or if you can't find it, you can put the file in the raw resources and read the file into a string and then use:
webView.loadData(htmlString, "text/html", "utf-8");
In any case, most probably the problem you have is you can't find the file. So make sure you are placing the file in the proper place in the assets, or resources. And you are accessing the file correctly.
Here is the documentation on how to access the resources:
https://developer.android.com/guide/topics/resources/providing-resources
If what you mean by local is on your computer, and you are hosting the file (which I assume would be just for testing), then you need to connect the android device / emulator to the same local network as your computer and then access via the local ip of your computer.
And here is another question similar to this that has already been answered:
Load HTML file into WebView
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 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
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")