How do I get the content from an asset loaded with the extension?

See below for discussion moved from the RUSH topic

1 Like

If I build an extension that generates an html file for display in a webviewer, can I add files to the assets e.g. css stylesheet, that will be picked up by the generated html? I could always pull in the content of the css stylesheet and include it as a variable.

5 Likes

@TIMAI2 May I suggest you a thing?

You can do like create a webiew and add in a view and do webview.loadData("your stuff","text/html", "UTF-8"); and put stylesheet in <style> element and at last, javascript stuff in <script> in your html string

4 Likes

You can do that, it will store it in a subfolder in assets named after your extension. Try to use only lowercase characters for the file. You can then later access the file by <package_name>/<asset_file>.something and read the input stream by context.getAssets().open(<target>).

3 Likes

@Know_About_IT and @Kumaraswamy, thanks for your input.

In a normal browser setting, the html would call the stylesheet like this:

<!DOCTYPE html>
<html>
<head>
	<title>ImagePDF</title>
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="stylesheet" href="mystylesheet.css" />
</head>

where the stylesheet is in the same directory as the html file.

Now there is either some magic where I can replace the

href="mystylesheet.css"

with something like:

href="/<packagename>/assets/mystylesheet.css"

which will work?

The alternate is to just call in the the stylesheet and set it to a variable

"href=\"" + ssVar + "\""

but how to call in the stylesheet from the assets ?

1 Like

Maybe you can try loading the CSS file from some local Url.

href="file:///android_asset/<package_name>/css.txt"

This should probably work (not sure fully).

Else another way of doing this would be to read the CSS file from assets to a string form and just to append like a CSS way.

1 Like

You can also load the webview with the base URL pointing to the asset directory and that would work too.

1 Like

and that path would be ?

This one probaly:

file:///android_asset/<extension_package>/

The stylesheet file is in the assets directory of the extension....

I believe the best way will be to load the file contents and save to a variable, which I can then add to the html. The html file gets created and stored to a temp file, so would need a relative path to the stylesheet file which will not necessarily be the same on all devices.

How, therefore, to load the file contents of a file in the extensions assets directory ?

Hmm, maybe we should move these posts to a new topic.

Anyway, this is how I do:

InputStream assetStream = context.getAssets().open("xyz.kumaraswamy.dynamicloader/encoder.jar");
        try {
            byte[] bytes = new byte[assetStream.available()];
            assetStream.read(bytes);
           ...
            assetStream.close();

After you get the bytes, you decode it as a string by final String text = new String(bytes).

I don't think so, this is specific to RUSH, accessing assets within the extension.

1 Like

hi @TIMAI2 you can add the JS script and the CSS script inside the HTML file as I have shown in the example code and then you could do one thing, you can use this URL data:text/html+your HTMLcode`

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <p>this is an exampel</p>

</body>

<style>

 /* your css code here */

</style>

<script>

    // your javascript code here

</script>

</html>

try opening this in a browser :point_down:

data:text/html,<html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p>this is an exampel</p> </body> <style> /* your css code here */ </style> <script> // your javascript code here </script> </html>

Thanks, will give this a go and report back.

@Yash_Sehgal22
I already have a method for getting either a local file or an alternative from the internet. The issue was about retrieving content from a file in the extension's assets. Thank you for your input, it may help others...

2 Likes

hello timai2, can u provide me files u want? U dont really need to add in assets, if u can provide files I can explain

for stylesheet, u dont need any stuff, simply add css in <style> tag in html and load html in webview

any way how can load assets data in web view?

It is a css framework 235 lines long, I wanted to avoid placing it between style tags....

save/copy css/js file in ASD, and save html also there