How to write in a file declared in res/raw? - android

I have a file stored in res/raw, and I would like to know how to wite into that file.
I tried getResources but it returns an inputStream.

Not possible. If you need to copy an asset to the device store the file in the assets directory. Then use the AssetManager to stream in and copy the asset so you can write it to disc or application cache.

You shouldn't be writing your application resources. Instead, try copying the resource to your writable data directory and modifying it there. There a number of methods on Context that will help you find an appropriate directory, depending on your needs.

Related

Save a (.txt) file to the Assets Folder in Android?

How to write to .txt file in assets folder.
How to get path assets folder ?
You can't. The assets folder is read-only at runtime.
Pick a different location to save your data, see Data Storage for more information.
The path of assets folder is file:///android_asset But this is read-only.
The assets folder is like folders res, src, gen, etc. These are all useful to provide different files as input to build system to generate APK file for your app.
All these are read-only while your app is running. At run-time you can only write to SD card.
I hope you understand my answer.
Not possible,
Better you choose internal storage to save your txt file. Internal Storage. Hope this will help you to understand.
No you can't do this, because of if you read and write both at runtime then everything could be change and application will behave unexpected which could be harmful or may be crashed. i suggest you to use internal memory, sqlite and app SharedPreferences etc.

Can i move a file in an asset folder to an asset sub-folder?

i have a file in the asset folder, and when i press a button in my program i have to move that file in a sub-folder(run-time). Can it be done?
i have to do this:
assets/file.txt -> assets/aaa/file.txt
No your assets are compiled into your apk file. Consider storing a persistent preference marking whatever you are trying to do instead.
No, you can't do it. You can't write anything inside the assets folder programmatically. Better option is to use internal storage. You can use Files Folder in the internal storage. See getFilesDir() in the docs.
Reason: Once you have an .apk file it is read only, so you can't add, delete or modify in it,
Alternate:
though you can use the sqlite DB for the purpose... where you can save file in hierarchy using blob type and you can modify it through queries...

Android Data paths

I am trying to make a file containing the folder res/raw so I can use listFiles() so I can index all the files in that folder... I tried using
File folderDirectory = new File("/data/data/com.soundboard/res/raw/");
file[] soundFiles = soundDirectoory.listFiles();
ListFiles returns null, I think, unless I am reading my debugging info wrong (the soundFiles path is null)
What am I doing wrong?
You could use cd /data/data/com.soundboard in adb shell to find there is no res/raw there.
Usually, res/raw in .apk should be static, so you could unzip .apk simply and find out what are in res/raw.
res/raw can not be opened as a File and used as a directory. There is no way to index the files in that folder.
EDIT: when you build your APK the contents of the res/raw directory are compiled into an efficient format. See How does the mapping between android resources and resources ID work? for more detail. The best you might be able to do is use reflection on your packages R.raw class and list all its fields: I've never seen this done, so whether it will actually work is anyone's guess.
Sounds like you should be using /assets and the AssetManager. It behaves exactly like a file system, so you can use list. The /assets and /res/raw serve much the same purpose, with the one big exception is that /assets can be accessed as a file system.
Otherwise as Femi mentioned, you could use reflection and get the fields for com.soundboard.R.raw. You could then iterate over those and generate file descriptors for each file there.

Adding file to .apk

My program needs large .txt file to be stored on SD-card (so, I want to redistribute it with .apk, without creation from the program). How I can attach the file (created on PC) to .apk?
You can save it in /res/raw folder
If you save your file as yourfile.txt
InputStream inputStream = this.getResources().openRawResource(R.raw.yourfile);
As far as I know you can open a .apk with zip and add files. Where is no magic :)
It might be slightly easier to use the AssetManager. Just save your files in the assets/ directory (as opposed to res/raw/). Then you can access them directly using their filename. http://developer.android.com/reference/android/content/res/AssetManager.html describes how to access the assets.

How to write to a file saved in local folder in Android

I am having a file in the folder res/raw/a.xml.
I want to write some data to this file?
How it can be done in Android?
How can we access a file stored in local directory in order to write data to that file.
can anyone help me in sorting out this issue ?
Thanks in Advance,
Hi, I am having a file in the folder res/raw/a.xml. I want to write some data to this file? How it can be done in Android?
You cannot modify a resource at runtime, sorry.
How can we access a file stored in local directory in order to write data to that file.
Use getFilesDir() to get a File object pointing to your app's local directory. Then, use standard Java I/O.

Categories

Resources