I'm using Android Studio Dolphin 2021.3.1
I have a WebView setup as follows:
val wb = WebView(this)
wb.loadUrl("file:///android_assets/html/About.html")
setContentView(wb)
I have an asset folder setup as follows:
[asset folder structure][1]
in the project disk location:
MyApp/app/src/main/assets/html
but when the WebView is displayed i get
net::ERR_FILE_NOT_FOUND
Can somebody suggest how i can access the files like this? do i have the location on disk wrong?
[1]: https://i.stack.imgur.com/c5Ury.png
Use url like this "file:///android_asset/html/About.html" with android_asset instead of android_assets
Related
I want to read a local html file to open it in Android WebView.
Why does the following work:
webViewMain.apply {
loadUrl("file:///android_asset/index.html")
}
But this code
webViewMain.loadDataWithBaseURL("file:///android_asset/",
URL("file:///android_asset/index.html").readText(Charset.forName("UTF-8")),
"text/html",
"UTF-8",
null)
gives me the following error: java.io.FileNotFoundException: /android_asset/index.html
The android file should be in the assets folder which is part of the root directory of the project.
assets/index.html
If you see the structure in the explorer it will be
/app/src/main/assets/index.html
and call webViewMain.loadUrl("file:///android_asset/index.html");
Things to check:
Make sure your assets folder spelling is right
The file is in the correct directory since it's giving you a FileNotFoundException
I wrote a script that allows you to read and write to a json file. As long as I tried the script in unity editor, I didn't have any problems. But when I switched to installing the application on an Android device, I can't find my json file.
The json file is called "playerData.json" and has been placed in a folder called "playerData.json".
This is the code for the path of the json file (working on unity but not working on android)
path = Application.streamingAssetsPath + "/playerData.json";
I tried using this string of code to get the code to work on android as well, but there is no way to get it to work.
path = System.IO.Path.Combine(Application.streamingAssetsPath, "/playerData.json");
I only wrote the line concerning the path as my android device cannot find the JSON file. Thanks everyone for the help!
Application.streamingAssetsPath does not work on Android and WebGL
It is not possible to access the StreamingAssets folder on WebGL and Android platforms. No file access is available on WebGL. Android uses a compressed .apk file. These platforms return a URL. Use the UnityWebRequest class to access the Assets.
There is a workaround though as mentioned using UnityWebRequest, can find an example in this Unity Answer
I'm using an iframe inside my home.html as follows.
<iframe src='../../assets/chatbot/chatbot.html' width='100%' height='80%' border='none' frameBorder='0' seamless='seamless'></iframe>
It works fine when running in localhost in a web browser. The problem comes when I build apk and installed in the android device. It says
The webpage at
file:///assets/chatbot/chatbot.html could not be loaded because:
net::ERR_FILE_NOT_FOUND
How can I specify path to this html file inside iframe?
found the issue.
specifying the path as follows solved the problem.
src='assets/chatbot/chatbot.html'
I want to load a local web content on a webview. I'm downloading a zip file, unzipping it and saving the files (main.html and resource files - css, js, fonts, pngs, etc) on internal storage (/data/data/<app>/files/).
All files are in the same directory:
|- main.html
|- file.js
|- ...
So, the html file points resource files as follows:
<script type="text/javascript" src="file.js"></script>
I though this would work
String path = context.getFilesDir() + File.separator + "main.html";
webview.loadUrl("file://" + path)
but the webview shows file not found error in pre lollipop devices, and in lollipop devices shows:
"The webpage at file:///data/data/<app>/files/main.html could not be loaded because: net::ERR_ACCESS_DENIED"
If I load the html as a string by running
String file = htmlFile.fileToString();
webview.loadDataWithBaseURL("", file, "text/html", "utf-8", "");
it works, but I have to resolve the dependencies by providing the full path of all resource files. There must be a better way to deal with this.
Does anyone know how to load a local file on a webview that was stored at runtime in Android?
Thanks
I forgot the config methods of the webview.
webview.getSettings().setAllowFileAccess(false)
This was the problem. By turning to true the webview is able to load local files.
I need to open a pdf in android mobile device.
For that ,I have add a 'test.pdf' pdf file in common folder and add plugin (Childbrowser ) in config.xml file (res/xml/config.xml). And again add childbrowser.js and cordova.js file in js folder .
And add a code to open a pdf file here---
window.open('./test.pdf','_system','location=yes');
But still exception is coming like that 'Target file is not available' andgive a path
file.///data/data/
I want to load that test.pdf file in the android mobile and open that particular file .
But it is not opening .
Please suggest me a solution. Thanks
Unfortunately, Android does not support viewing PDFs out of the box in a WebView. Most results point to using Google Docs.
Here are some suggestions:
Open PDF in a WebView
How to open local pdf file in webview in android?
http://developer.appcelerator.com/question/147570/open-remote-pdf-file-on-webview-in-android
This you could do by combining a Cordova plug-in and this native code: http://kylewbanks.com/blog/Loading-PDF-in-Android-WebView
http://asmncl.blogspot.co.il/2012/06/android-open-pdf-file-in-webview.html