Load HTML file from a linked java library into Android webview - java

I have a webview into which I'd like to load an HTML file that comes from an external java library.
That is, my project includes a java library that, besides a bunch of Java classes, also contains a bunch of HTML (and related) files inside its resource folder. I'd like to load these files into my webview.
I know how to load HTML files when they are located in the assets folder: you simply run webview.loadUrl("file:///android_asset/your-file.html").
I do not know how to write the url to access resources in the linked JAR. The docs do not help much.

Related

Convert a simple html file to scorm package

I want to host my html asset into LMS, here LMS only accept and run the SCORM Package.
How can i convert an html file (exporting from my app using jasper report) to a scorm package?
Any help?
For single pages, in fact the best approach I found was to do it mannually: based on: https://www.ispringsolutions.com/blog/how-to-convert-html-to-scorm to adapt it, just ignore the res folder and point to the index on: <resource identifier="resource_1" type="webcontent" adlcp:scormtype="sco" href="index.html">
Download a SCORM example: https://21w98o3yqgi738kmv7xrf9lj-wpengine.netdna-ssl.com/wp-content/assets/golf_examples/PIFS/ContentPackagingSingleSCO_SCORM12.zip
extract it;
in imsmanifest.xml change the title to your title;
List all the resources that your HTML page includes;
Zip the course root folder (with the manifest file).
On this page you will find software that you can use to create SCORM compatible packages. If you have created a simple project, you can unpack the resulting ZIP file and edit the content. Mostly you will find an index.html there. I know, that sounds a bit awkward at first.
https://docs.moodle.org/35/en/Creating_SCORM_Content

How do I access a file in this hierarchy?

I have this file hierarchy:
Java Resources
src
model
Class.java
Web Content
datos.txt
I need to access from Class.java to datos.txt. I tried using C:\Users\Tomi\Documents\Eclipse\ProyectoWeb\WebContent\datos.txt
but when I use this application in another computer, it doesn't work. How can I modify that direction?
Use relative paths. For example if your application is at C:\Users\Tomi\Documents\Eclipse\ProyectoWeb then use WebContent\datos.txt as the path.
Then even if you move your application it will still look for the folder WebContent in the folder the application is running in then datos.txt within that.
For a more portable way you could embed it in the classpath an access to it via classloader, see Access to resources (images) inside jars/classpath for example or Unable to load picture from resource

Loading static resources from inside one-jar

I have a web page hosted on jetty server. The page displays some static images, the images directory are kept inside a jar, say application.jar. This application.jar is packaged inside a one-jar. I am facing problem in accessing the images directory as they are inside a jar which is inside another jar.
I have tried almost all the ways to get URL of the images directory: class.getClassLoader().getResource(), Thread.currenctThread.getContextClassLoader().getResource(), ClassLoader.getSystemClassLoader().getResource(), etc. None of them is of any help. I wrote all these statements, i.e. tried to get access to the images directory, from a class that is inside the application.jar. This jar contains the images directory too.
If anybody has ever faced this before, please reply to this thread. I am open to any other ideas also that may help me achieve the objective.
You can use
InputStream input = getClass().getResourceAsStream("/classpath/to/my/file");
You can find more here:
How to read a file from jar in Java?
How to access resources in JAR file?

Applet: Loading image in jar file that is not in home directory

this title may sound confusing so I'll explain my problem.
I am converting my java application to an applet, and it works fine in Eclipse and when I make a html file with tags. But when I upload my applet to google sites and execute it via a html file, the program itself loads fine, but it tries to load its images from my own hard disk instead of from the JAR file. How can I change that?
HTML:
<applet
archive="https://sites.google.com/site/projectteagame/tea.jar"
code="ymte.core.Loader"
width=300
height=300> </applet>
Edit:
I keep all my files in the jar, for speed and ease. My code to obtain the images is:
img = ImageIO.read(Loader.class.getResourceAsStream("/res/object/ball.bmp"));
This works in my IDE, but not in the online use
You can provide your code to tell us how you obtain the image, but I suppose it is not bundled in the jar. A way to do that is to put the jar in your packages and obtain it like this:
Class.getResourceAsStream("foo");
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Class.html#getResourceAsStream(java.lang.String)
This is kinda gimmicky, however. A preferred way would be to put your image into a directory in your project that is outside your sources, something like images or resources and then bundle it in the jar of the applet. Use your favorite build tool (ant, maven, ivy) to include the image in the jar. Directory structure can be like this:
project
-src
-resources
-<yourImage>
Then you would access your image as a URL or File using a relative path. If you provide more code, maybe you can receive further help.
EDIT:
SInce your resource is in your applet, you can put it in one of your packages. Then, further in your code you can do
Class.getResourceAsStream
and that is how you can get your image, since it is guaranteed to be in your classpath.
Basically, if you have a package structure like this:
project src
com.foo.bar
com.foo.bar.stuff
com.foo.bar.resources
-yourImage
-otherStuff
-ResourceGetter.java
You can do the following in your ResourceGetter
Class.getResourceAsStream("yourImage");
Which will give you a handle to your image and you can load it as you would do normally.

Java applet error

I am doing a project on applets. I designed the applet using netbeans. After building the project in netbeans, I took the directory "classes" and a .html file from the "build" directory and moved it to another new directory. This .html file includes the applet. The .html file displays the applet correctly, when it is viewed from my desktop.
I uploaded the "classes" folder and the .html file to my free server (host4ufree.com) using FileZilla. If I try to view the webpage online, I get the following error instead of the applet getting displayed:
java.lang.ClassFormatError: Extra bytes at the end of class file
I am using JDk 1.6.0 update 18, and uploaded the file using FileZilla both ASCII and binary format manner. Yet, I am not able to solve the error problem. Does anybody know the solution to this? Is there something wrong in the manner in which I'm trying to add the applet to my webpage?
The question is quite unclear :S Anyway...
I uploaded the "classes" folder and the .html file to my free server
(host4ufree.com) using FileZilla.
If your applet contains more that one class I do not recommend upload the project classes folder itself but wrap your applet classes to jar file before delpoying it.
Report if that helped

Categories