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.
Related
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
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 working an Android application. Some activities, I used webview and load html from Android asesst folder. On apk if rename package to name.apk.zip then anyone can easily access my asesst folder content.
Now I want to protect my assect content mainly html files.
Please help to give some suggest about html encryption or Android studio plugins about protection.
Most efficient way to do this, it's include that files in your Server, and getting files by authorization.
To do it locally. I suggest to create custom task in gradle for mapping your html/css files in some incode storage, with Base64 (encoding just for ex.). It's looks like:
task mapAssets(dependsOn: build) {
SOURCE_FILE = 'Path to Source File'
doFirst {
println "I'm Gradle"
}
String source = new File(SOURCE_FILE).text
ENCODED_FILE = 'Path to Encoded File'
new File(ENCODED_FILE).withWriter { out ->
out.println source.bytes.encodeBase64().toString()
}
}
Later in your Java code, just find ENCODED_FILE and make call with reading file and mapping again in HTML, from Base64 (or any other encoding).
This code works in the simulator but not on my Android device:
local path = system.pathForFile("chinese_rules.db")
print("PATH:: " .. tostring( path ) )
When I run this code on my Galaxy S4 path returns nil.
My first thought was that it was some typo (case sensitivity) but I can't find any typo:
http://i59.tinypic.com/wlpu14.png
I can't find any reason why it should receive nil. This causes a problem as I can't load my database.
I have also tried this with the same result:
local path = system.pathForFile("chinese_rules.db", system.ResourceDirectory)
I have been able to load a path and load databases like this before.
Corona Build: 2013.2100 (2013.12.7)
Further reading the documentation I don't see that .db is a restricted file type:
Corona allows direct loading of images and audio files using the
appropriate APIs, but it has limited access to resource files on
Android using the file I/O APIs. Specifically, the following types can
not be read from the resources directory: .html, .htm., .3gp, .m4v,
.mp4,.png, .jpg, and .ttf.
http://docs.coronalabs.com/api/library/system/pathForFile.html
I found out the reason for the problem:
We are two that are working on this project and he had setup to use expansion files so two files was created (the main APK and the OBB expansion file) which I didn't notice and I only loaded the main APK file and I guess the database is in the OBB file. After setting not to use an expansion file the app works.
usesExpansionFile = false
I'm write a Android web App Use jQuery mobile/HTML5/Use Phonegap/Eclipse/jqmphp, jqmPhp is a package of PHP classes, it echo the HTML dom elsements, I see its source,
define('JQMPHP_JQM', 'http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js', true);
But I want host the JavaScript and CSS in the APK file to reduce the HTTP request. How Can the HTTP web call the APK's resource?
assets/www/foo.js -> <script src="./foo.js"></script>
You can host the files in the android assets folder, just include the files in regular script tags in your index file.
Also, there are newer, more stable versions of jQueryMobile available.