access files from assets/www directory

后端 未结 3 1153
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 11:34

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

相关标签:
3条回答
  • 2020-11-28 11:52

    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

    0 讨论(0)
  • 2020-11-28 12:04

    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.

    0 讨论(0)
  • 2020-11-28 12: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

    0 讨论(0)
提交回复
热议问题