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.
Related
In my flutter project, I want to download some files from a url and save particularly in a folder named data. I have added Read and Write permission in AndroidManifest.xml file. I have also tried some solutions like the below link-
flutter - How to download asset when apps launched and used it
But none of them was effective for me. some of the solutions let me save the file in SD card but I only need to save the file in my data folder and read data from the file whenever I need.
Here's the picture where I exactly want to save my file-
So, I need a proper solution to download the file from the url and save it to this particular directory.
https://pub.dev/packages/download_assets if you don't want to add assets at time of build try this package it download asset from server and store it in assets
I am using the below code to create a file in android as
File file = new File(getApplicationContext().getFilesDir(), "content.txt");
I want to open the file in my phone. I went to the path
Local storage/Android/data/<my package>/files/
However, I cannot see the file. My Android version is 5.0.1. What is happen? One more thing, Do you think the above way is a good way to save file? What is common path to save a file in Android? Thanks
The file locates in /data/data/[your package]/files,it's app private directory.
It's a good way or not depends on the purpose of your file.
if you want your file visable only to you(the app developer),the folder is right
if the file can be visable to others and the file is large,then you can put it to sdcard folder
if you want the file stay even if your app being removed,you shouldn't put it in this folder.Because when an app removed,the all data in /data/data/[package name]/ will be deleted also.
You find in:
/data/data/[your package name]/files/
I have code for creating an internal file, there is random algorithem that create the data stored in it and i want any app to have the same file with the same binary data in it.
so i need to make the file on my desktop and add it to internal files some how.
my question is what do you think is the best way to do it.
i thought to locate it in my project, read it, and write it to internal files.
the problem is, i dont know where to locate my file in android studio so that it will be included in the external files and then where to read it from.
thanks. =]
hope i made myself clear.
Put it in src/main/assets/.
You can then access your file with AssetManager and do whatever you want with it.
From the Android Developers website:
main/assets/
This is empty. You can use it to store raw asset files. Files that you
save here are compiled into an .apk file as-is, and the original
filename is preserved. You can navigate this directory in the same way
as a typical file system using URIs and read files as a stream of
bytes using the AssetManager. For example, this is a good location for
textures and game data.
You need to move that into the assets folder. From there you can refer to the file.
I have a short question about writing to a file in android. I am writing a game where I use a xml file to save some data about the level stats. Now I have seen that if I save this xml file in AssetManager it is not possible to change it (only permissions to read files).
Now because I can only modify files which are in the filesystem of android (using openFileInput and openFileOutput to work with it) I wonder where I have to save my (already existing) xml file in my eclipse project so that I can use openFileInput to load it and change it via code.
Do I have to make a new folder? E.g. project_path/files/myxml.xml.
Is it even possible to load a file which was created (outside the AssetManager folder) before installing the .apk to target?
If it is possible does anybody have some example code?
I hope you understand my question.
There is no such place. Installation of android apps does not include an automatic step that would copy your content from apk to the internal folder (and your application does not reside in the folder either).
You will have to create your XML file in code, possibly checking for its existence before each access (or using some other marker).
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.