i have a interactive map folder. i try to create a webview app with local files.
inside the folder are all the files needed to load the map.
gta5online.com/map-interactive
also included is a html file that i tried to open in an app. i just pasted the folder inside the main application folder for android studio.
i tried to put the path in the code as you can see for the string. when i run emulator it says it cannot find the file. what do i need to put there. can someone give me the exact one please? i'm a noob.
the path on my pc is: C:\Users\me\AndroidStudioProjects\MyApplication\interactive\map.html
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url ="file:/interactive/map.html";
WebView view=(WebView) this.findViewById(R.id.webView);
view.getSettings() .setJavaScriptEnabled(true);
view.loadUrl(url);
The best way to reference html content in this case is to put it in an ./assets folder and use the URI file:///android_asset/ to refer to it.
See for example the reply here: How to get URI from an asset File?
To be fair, I haven't tried that since Android Studio became a thing and gradle changed the default project layout -- hopefully that doesn't further complicate matters for you.
Related
So I have been at it for a while, and I really cannot get it to work. What I want is this:
To access some file outside the project folder through a WebView.
Here is some code that I have been trying to get it working:
String url = "file://///\\myProjectName" + Environment.getExternalStorageDirectory().toString()+ File.separator + "hw.html";
WebView webView = (WebView) findViewById(R.id.WebviewLOAD);
webView.loadUrl(url);
I just get a ERR_FILE_NOT_FOUND with any type of file. For a test-HTML file I created, I get this along with file not found error:
"The webpage at file:///myProjectName/storage/emulated/0/hw.html could not be loaded because"....
I tried removing Environment.getExternalStorageDirectory().toString() but then i just get file:///myProjectName/hw.html along with the file not found error.
So I want to navigate to the right file and be able to open it, but I just can't seem to get it right. I'm open for any suggestions?
So, basically you can't do this. Instead you want to run a local server in the background and make the WebView and server work together.
I did it with Jooby with an SQLite database.
I'm using AIDE (A code editor / IDE & compiler which runs on Android) to write an Android app, and I want to add some assets to my project, specifically a HTML file plus associated CSS and JavaScript files, to be displayed in a WebView.
I've put my assets (a HTML file) in projectDirectory/app/src/main/assets/htmlfile.html, but, when I use the code below to load it in WebView, it shows "File not found". The exact same thing works fine in Android Studio.
Where do I put this assets folder for AIDE to pick up its contents ?
Here is my code where I'm loading the WebView, although I don't think there's anything wrong here, as it works in Android Studio.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webView = new WebView(this);
setContentView(webView);
webView.loadUrl("file:///android_asset/htmlfile.html");
}
What I've tried:
Placing it in /app/src/main/assets/htmlfile.html (The usual location, which works in Android Studio).
In /app/src/main/res/assets/htmlfile.html
In /app/src/assets/htmlfile.html
Pretty much everywhere else.
However, wherever I put it and recompile, the WebView always shows "File not found". So where do I put my assets folder for it to work with AIDE, or how do I make AIDE pick it up when compiling ? Does AIDE support assets at all ?
Doh: All I needed to do was menu > more > project > refresh build, with the assets in /app/src/main/assets/htmlfile.html and it worked. Somewhat non-obvious, but hey, solved my problem.
Leaving this self answer in case anyone else has the same problem.
As for an html keep it in app->src->main->assets
And later on you can acces it like "file:///android_asset/error.html"
Lets take example of a webview wv1. You can write
wv.loadUrl("file:///android_asset/error.html");
Dont forget to rebuild your project. This is a safety measure in such case. Many times files are not included automatically
Being very new to android development I am asking for some help with a problem I have. I need to view an HTML file through a Web View on mobile devices. The HTML file, 3 in total, would be saved on the device. My question is where would I put these files and how would I call them. I know that it can be done on an iPhone as I have done it, but as I know next to nothing about Android and I am in need of guidance. Many thanks for any help.
Put your HTML in the assets folder and just:
WebView view = (WebView)findViewById(R.id.your_web_view);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl("file:///android_asset/your_file_name.html");
If you don't have an assets folder, just create it:
Right click on your app -> New -> Folder -> Assets Folder
I want to know what does file:/// mean while loading a html file from the assets folder in android
Is it an absolute path name which points to the root directory?
I saw this in the tutorial for phonegap by the way.
file:/// is a URI (Uniform Resource Identifier) that simply distinguishes from the standard URI that we all know of too well - http://.
It does imply an absolute path name pointing to the root directory in any environment, but in the context of Android, it's a convention to tell the Android run-time to say "Here, the directory www has a file called index.html located in the assets folder in the root of the project".
That is how assets are loaded at runtime, for example, a WebView widget would know exactly where to load the embedded resource file by specifying the file:/// URI.
Consider the code example:
WebView webViewer = (WebView) findViewById(R.id.webViewer);
webView.loadUrl("file:///android_asset/www/index.html");
A very easy mistake to make here is this, some would infer it to as file:///android_assets, notice the plural of assets in the URI and wonder why the embedded resource is not working!
If someone uses AndroidStudio make sure that the assets folder is placed in
app/src/main/assets
directory.
The URI "file:///android_asset/" points to YourProject/app/src/main/assets/.
Note: android_asset/ uses the singular (asset) and src/main/assets uses the plural (assets).
Suppose you have a file YourProject/app/src/main/assets/web_thing.html that you would like to display in a WebView. You can refer to it like this:
WebView webViewer = (WebView) findViewById(R.id.webViewer);
webView.loadUrl("file:///android_asset/web_thing.html");
The snippet above could be located in your Activity class, possibly in the onCreate method.
Here is a guide to the overall directory structure of an android project, that helped me figure out this answer.
It is actually called file:///android_asset/index.html
file:///android_assets/index.html will give you a build error.
It took me more than 4 hours to fix this problem. I followed the guide from http://docs.phonegap.com/en/2.1.0/guide_getting-started_android_index.md.html#Getting%20Started%20with%20Android
I'm using Android Studio (Eclipse with ADT could not work properly because of the build problem).
Solution that worked for me:
I put the /assets/www/index.html under app/src/main/assets directory. (take care AndroidStudio has different perspectives like Project or Android)
use super.loadUrl("file:///android_asset/www/index.html"); instead of super.loadUrl("file:///android_assets/www/index.html"); (no s)
it's file:///android_asset/... not file:///android_assets/... notice the plural of assets is wrong even if your file name is assets
I'm new to Android development.
I want to load a html file into a webview.
Note that there are so many relevant questions on SO like this, but they all deal with getting **.html* from assets folder.
But I want to load html file from local folder, say "D://abc.html" because if my html is around 10Mb then corresponding apk size also goes upto 10mb.
Any help appreciated.
EDIT
I tried webView.loadUrl("file:///D:/Projects/myWebsite/index.html");
but it gives Web page not available and File not found error.
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)
In Android 4.4 KitKat, a "Not allowed to load local resource: file:///.." is thrown.
arise when loadURL and the only alternative I've found is "loadDataWithBaseURL".
webView.loadDataWithBaseURL("file:///android_asset/demo/",
tmpDocumentText,"text/html", "UTF-8", null);
WebView has loadData method http://developer.android.com/reference/android/webkit/WebView.html
All you need to do is reading the file into String then feed it to WebView using loadData.
either one can use this way to load file specifically if the file is present within android studio project
(pls note android_res is res folder actually and should not be replaced)
webView.loadUrl("file:///android_res/drawable/FILENAME.EXTENSION");
but if that is not working please try another way of loading file like
webView.loadDataWithBaseURL("file:///android_asset/demo/",tmpDocumentText,
"text/html", "UTF-8", null);