• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps WebView and the Android file system

Hello.

I want my android application to create a .js file, which can then be read by my web view. I have read that the "assets" file is ready only so I cannot save a file to there during runtime. I am saving it in the default "/files/" directory.

I want to be able to reference this file from my webview HTML file. The webview is showing local html files stored in the assets folder. What is the file directory I should use to reference this the .js file in the .html file?
 
Best bet is to save it to a known directory, such as /sdcard/.AppName/ and hsrdcode the rel tag in your html file. Or, put the html file in the same directory as your js file, and use a relative path in your rel tag.
 
I want to store it on the internal memory if possible. How do I store the files somewhere that isn't in the assets folder? Thats the only thing that gets compiled into the apk isn't it?

Sorry if this is a stupid question - I've been looking all day!
 
To any boyd viewing this topic looking for an answer, you do indeed have to copy the file over to the /files/ section - I used this code:

private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for(String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
out = new FileOutputStream("/data/data/package_name/files/" + filename);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
 
Back
Top Bottom