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?!)
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
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 - "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.
Related
I want to host an HTML file on server and once user is registered on the app, i want to get this file downloaded and saved into assets folder of the app on the local device and once i have this file into assets folder, i will display it into a WebView and user should be able to see the contents of that file every time user opens it.
And whenever i update the file on server, i want to display a popup for user to accept it, and once user have accepted it, i want to replace the old HTML file from assets folder with the new one.
And from now onwards, the new file will be downloaded on the local system and every time user wants to access the file, he should be able to access the new one.
I'm not sure if that's possible at all, but would certainly be interested in hearing from anyone that knows anything about such things.
Since I cannot write any files to assets or any raw dir, where it will
be written?
openFileOutput() writes to internal storage.
What will the location of that file be? Where will it be located in
the Android file system ?
The apk is read only and the assets/ folder is not modifiable at
runtime, which sounds like something you may be seeking.
Link Source
After looking all over google I haven't found a way of accessing Android internal storage from python.
I need to store an image generated by kivy app. I would like my python code to be able to navigate to a root user dir, create an app specific dir (if not found) and save the image there.
Simply creating a file with my device's path doesn't work. Should I be setting permissions for internal storage access? I'm lost with it.
You have something wrong in your code, you didn't paste the code nor logs, so... let's get it another way.
You can read/write with python just fine in the app folder (/data/data/org.something) with using:
app_folder = os.path.dirname(os.path.abspath(__file__))
To write to SD card you'll need to use App.user_data_dir, which as you can see in the docs on android gets you /sdcard/<app_name>. Not sure if it automatically creates it if it's missing, so it needs checking out probably. However, if it doesn't exist, just create it with python:
os.mkdir(App.user_data_dir)
I am new to android and facing issue with file system usages.
I have seen several threads but i am still not able to find the answer which i am looking for my requirement.
I have a original file say test.xml which i want to keep somewhere in my eclipse project so that as soon as i install the app it should move my test.xml file into data directory so that i would be able to read it
please refere this link :
http://developer.android.com/training/basics/data-storage/files.html
I want to save my original file at : "/data/data/com.example.helloWorld/files"
I should be able to get the file handle form the below command
File file = new File(context.getFilesDir(), filename);
Please Note i dont want to save my file in asset folder.
Thanks,
Manish Bansal
I recommend you use assets as storage for your file initially as that will get compiled with your app, then once the user starts your app you check if your file exists and if not you can save the asset to the internal storage location as specified in the documentation, from there you can use the file as you like in the filesystem.
While trying to write a simple file to the applications file directory with phonegap-1.0.0 I get an error with code 5 - NO_MODIFICATION_ALLOWED_ERR. The method where I get the directory is like this:
function gotFS(fileSystem) {
fileSystem.root.getFile('file:///data/data/package.name/files/ff.txt', null, gotFileEntry, fail);
}
As far as I know, the files directory should be writable by the application, and writing to it with a BufferedWriter was successfull. I tried only leaving file name, without file:/// and data/data/package.name/. I tried many different versions of Phonegap and many methods only to read/write a file and seems like nothing works. Any hint would be appreciated.
The "files directory" should be writeable, but that isn't necessarily it:
Android could decide to place application storage under a path other than /data/data/package
The installed name of the apk can be different, for example it can have a numeric suffix
There may not (yet) be a directory called files/ within the application storage
You should use the Android API function getFilesDir() or getDir() to get a directory where you are allowed to write from that particular installation on that particular device. Presumably phonegap provides you some means of obtaining the result of one of those functions.
You shouldn't hardcode the path like this. Use getFilesDir() or getDir() functions to get the path instead.
I am trying to load an xml file located in the /assets folder of an android project by name using this method:
getAssets().openXmlResourceParser("thefilename.xml");
However, executing this code always throws a "FileNotFound" exception, even though the file is located in the /assets folder and is with the correct file name.
Now, I have not put the file in the /res/xml folder because I really need to be able to 1. edit the file right on the device itself and most importantly 2. add new xml files to the application without issuing an update, to allow for easy user modifications.
Thanks in advance.
I think what you are looking for is either getAssets().open("thefilename.xml") or getAssets().openFd("thefilename.xml") depending on what the end use of the file is. You can see from Dianne's response in this post awhile back that openXmlResourceParser() is not really usable just to gain access to files in the assets/ directory: http://goo.gl/2KfgT
From there you will have a stream that you could feed into a SAXParser or do whatever else you choose.
Side Note: On the points you mentioned you really can't edit files directly in assets/ or add new files to assets/ at runtime. You will need to work with files on either internal or external storage to do those things. You probably already knew that, but I thought I'd mention it.
Hope that Helps!