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
Related
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 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.
I am working on some app that loads external swfs from the application directory
the problem is that some swf files get loaded correctly and others give ioerror url not found
i put the paths in an array and use a loader to load the path
var arr:Array = ["Games/1.swf", "Games/2.swf"];
var loader:Loader = new Loader();
loader.load(new URLRequest(arr[0]));
this is just example and it is the same in loading all the files but does not work on all files.
what would be the problem?
Firstly, whenever you load something, listen for the appropriate error events so you know what's going on:
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
When using mobile, you should use the app:// shorthand as a reference to the application directory (which I assume is where your Game folder is. Relative paths do not usually work.
So it should look like this:
var arr:Array = ["app://Games/1.swf", "app://Games/2.swf"];
For more information/options, you can look at my answer to this question
So, I've seen many posts on this, but none has helped. I want to work with the assets folder, for organizational purposes. The images below show where the 'assets' folder is, along with the app.imi options of where it should be. I have tested this, and in the >exploded-aar/com../appcompat-v7/19.1.0/assets ... the files inside this seem to work. I can call on the .mp3 file and load and play it correctly (Though a few times it did not recognize it. Even when it was inside a folder, inside of the directory.)
In the src/main/assets folder, all the files are ignored. Is there a specific way I have to call this? In other words, must I also link the actual folder it is it? src/main/assets/music to load up the riseofcc.mp3?
I do not have 10 reputation to post images, so sorry for only links.
EDIT:Also I am using Android Studio 0.5.7
ANSWERED:
Thanks for Xavier Duchrohet for explaining exploded-aar is for the application's dependencies. I have learned that when calling upon a file inside your asset's folder using the code:
AssetManager assetManager = getAssets();
InputStream inputStream = null;
AssetFileDescriptor descriptor = assetManager.openFd("sound/explosion.ogg");
You must include the folder's parent inside the arguement.
Android Studio's app.iml has the asset's directory (src/main/assets) inside of its configuration's options.
Normally, to include sound files, I create a folder called raw, inside the app / src / main / res.
That is, I right click on the folder res and from the menu that appears, choose: New / Android Resource Diretory. Renaming in the window that appears, the Directory name field values for raw.
How it works for me, I hope this might have helped him.
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