I am trying to create an Android application that would use a configuration file (plain text) when loading.
When testing locally (with an emulator), I place my config file in the src/ folder (ecplise) and I see it is copied and used from the bin/ folder upon project build.
My questions are:
1) Where do I need to place this file when testing on a device ?
From what I understand I need the *.apk file and the config file to be both present on the device during run time.
2) If I am using eclipse to install the *.apk file can I somehow specify the config file as a dependant file and force eclipse to copy it as well ? If not do I need to manually copy the *.apk & config files to the device ?
NOTE: I am using getResourceAsStream to load the file.
Thanks,
RM
Well actually there shouldn't be any problems with accessing the file in a way you do. But preferred (and recommended by Android developers) way is to place the file into /res/raw folder. Then you can access the file as application resource:
InputStream is = getResources().openRawResource(R.id.your_file_name);
Here getResources is a method of Context class, it should accessible anywhere within your activities. R.id.your_file_name is a static field generated by eclipse at the time you place your file into /res/raw folder.
Read the https://developer.android.com/guide/topics/resources/index.html to get more about application resources at Android.
Related
My goal is to have /data/data/pkgname/files/mydir/myfile.txt
By default there are only cache and code_cache
I know that I can getFilesDir() in code and create necessary directories and files.
However is it possible to simply create necessary dirs with files in the project folder before building? (I tried creating this dir in root of the project, in main, src, nothing seems to work)
Thank you in advance!
However is it possible to simply create necessary dirs with files in the project folder before building?
No, sorry.
You can put a directory tree with files in assets/. However, those do not turn into files on the filesystem of the device automatically. You can access them via AssetManager, either to just use directly or to serve as the basis for copying that tree to files in getFilesDir().
I make android application that read file.txt from resource/raw by context?.resources?.openRawResource(R.raw.file), and it's work fine.
I have tried assets directory by : context?.assets?.open("file.txt") and it's work also.
Now, I need to delete this raw/assets resource file or delete the content. Is there anyway to do it with android code?
No, there is no way to update the Assets or Raw files.
Both of them are read-only.
What you can do, it's to make a copy of the file in a writable directory and modify them as needed.
UPDATE
Just to give more context, both assets and any resources are part of the binary file (APK or app bundle). As they come as part of the package you cannot change them without creating a new binary.
I have some files that I want to read from my app.Currently, I have to do the following:
Check if the file exists in the files directory.
If it doesnt, then copy the files from assets to the files directory
If it does, then, skip step 2
So, is it possible to not copy the files ? This increases the size of my app (uselessly) as there is an extra copy of all the files.
Note, that I have to access the file using getfiles(). I am using a library that doesnt work if I give the uri of my assets folder.
So, it it somehow possible to compile the app with some files already in the files directory ?
Internal storage for you app is created when application is installed so there is no way to provide files there during compile time, that would be magic.
You could try creating ContentProvider for sharing your files stored inside assets folder.
Besides, you should tell which library you are using. Then I may be able to suggest something more precise.
I want to add a text file to my APK root. This file will not be used in the application but it will stay there for manual extraction of the APK.
I tried to put it into the root of the project in Eclipse but it didn't include that file in the APK. I don't want to put it into assets folder. Can't I put it to the root?
Use the assets folder.
According to Android developer
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.
Files that are not put into any of the main directories of your android project will NOT be included in your apk. You MUST put the file in the assets so it can be accessed within the application as an external resource/file.
I'm pretty new to eclipse and Android and I'm trying to add some files to the assets folder, but certain files and directories are not being included in the .apk file.
I have set-up the following structure in the solution:
/assets
/textures
/test.png
/splash.png
/xml
/testData.xml
I don't know if there is a specific way to get eclipse to re-build/compile the project and add the new assets to the assets folder in the .apk, but the /xml subfolder (and its contents) are not in the apk. Subsequently I get FilenotFoundExceptions thrown when I deploy the app. The textures are all included.
How can I force a recompile and rebuild of the apk and is there anywhere to manually check which files are included in the project (like an assets manifest file)?
As far as I know there are no restrictions placed on hierarchy or file formats for within the assets folder. Also, when I dragged the files from the desktop to the solution explorer, I selected 'copy files to project'.
Thanks
It appears one cannot use upper-case letters in the naming of files within the assets folder. Contrary to my example above, I was actually using camel-backed file names within the folders. After changing the file names to lower-case, the apk was automatically re-built with the assets the next time I deployed