14

let's say I've got a file called foo.html sitting (quite comfortable) in my assets/www directory (next to my index.html).

I'd like to copy that file to another location on the device. My first approach window.resolveLocalFileSystemURI("foo.html", cool(), notCool());is not working. Also with a prefix like www/ it won't.

It would be interesting to know if it is actually possible at all to access files via Phonegap. I'm not convinced and therefore would like to see a code snippet how to obtain a FileEntry for files in the assets directory - if possible.

edit: Ok now we've got a call like this

window.resolveLocalFileSystemURI("file:///android_asset",
  function(entry){
    console.log(entry.fullPath);},
  function(evt){
    console.log(evt.code);}
);

but we've get an error with code: undefined (Phonegap v1.2) and code: 1 with v1.0 (code 1 = file not found?!)

nuala
  • 2,681
  • 4
  • 30
  • 50

3 Answers3

14

You can't do what you want to do. The files in the assets directory are not technically on the file system so they are not accessible via the File API. This means calling window.resolveLocalFileSystemURI() will not return you a FileEntry.

The best you can hope for is to access those files via XHR. If they are text based you can always take the result of the XHR and write it to the file system using the FileWriter. I wrote a blog post that shows how to get a file from the assets directory using XHR.

http://simonmacdonald.blogspot.com/2011/12/on-fourth-day-of-phonegapping-creating.html

Khalid
  • 4,730
  • 5
  • 27
  • 50
Simon MacDonald
  • 23,253
  • 5
  • 58
  • 74
  • I don't have the code here at the moment but I pretty much achieved what I needed to do with the accepted answer. Anyway nice to "meet" you Simon, I've read a lot posts by you in the phonegap groups :) – nuala Jan 23 '12 at 01:40
  • @yoshi, sorry, but were you able to access assets files using Leon's answer? Or even get the path to them? I was trying to make it work but I couldn't! – lao Nov 22 '13 at 17:58
  • lucaskoala - Were you able to get it to work? I'm not having any luck (and I'm thinking Simon is correct that it's not possible). See here: http://stackoverflow.com/questions/22948430 – Matt Grande Apr 08 '14 at 21:28
11

You should be able to access the file this way:

window.resolveLocalFileSystemURI("file:///android_asset/www/foo.html", onResolveSuccess, onFail);

You can then use the File API - FileEntry.copyTo or FileEntry.moveTo to actually do the action. Note - you cannot actually write into the assset/www folder, only to an SD card.

Hope this helps

Leon
  • 4,532
  • 1
  • 19
  • 24
  • 1
    it looks so nice but it's not working :C Always calls the fail-method. Even when I try to access the script.js file instead my dummy html. Also I've double checked the directories. The error event only has 1 field namely code: undefined. – nuala Nov 30 '11 at 13:50
  • 1
    anything in your logcat output? – Leon Nov 30 '11 at 13:54
  • logging the error event reveals only one field: `code: undefined` – nuala Nov 30 '11 at 14:02
  • are you using the latest PhoneGap build? I know they changed quite a few of those File API in the last releases... – Leon Nov 30 '11 at 16:23
  • ah yes. That explains why I got an error code 1 in a fresh project. Just tested it and was wondering about it. So with v1.0 I get error code 1 (not found?) while version 1.2 remains silent (code: undefined). I'll edit my question and put some more code in it. – nuala Nov 30 '11 at 16:53
  • I am just wondering if [this link](http://vikaskanani.wordpress.com/2011/06/11/create-file-explorer-in-blackberry-phonegap-project/) might help your further, sorry I don't have now an Emulator running so I can't test it locally... – Leon Nov 30 '11 at 16:55
  • I'll edit your answer by adding the 2nd option with the FileEntry constructor - it worked for me :) thanks a lot! – nuala Nov 30 '11 at 17:18
  • @yoshi can you still provide your solution? I know a lot of time has gone, but i can't seem to fix it :( – markusthoemmes Aug 07 '14 at 15:55
1

Leon - "you cannot actually write into the assset/www folder, only to an SD card" The first part is true, you can not write to the asset/www path. But, if the app is installed on the device, you can create and write to a file and it gets created at android's root. If the app is installed on an SD card, that file gets created at the SD card's root. Files thusly created are NOT deleted or altered when clearing user data for the app and are NOT deleted when the app is deleted.

icache
  • 41
  • 1