editing a text file packed in android apk - android

i am using a properties file to get urls to call web services in my app. Is there a way to edit that through some android activity.
The properties file is in src/resources folder. i am accessing that file using Resourcebundle.

You cant edit any file in application pkg file, however you can have following options to achieve this:
Have Preferences, if when you fetched preferences are null, load preferences with that property file, else let it be as it is.
Edit values in preferences.
If you still insist to use a text file only, copy property file to filesystem, and edit that file whatever you want to edit.

Related

Android Studio:Where to store a text file and the path for it

I'm developing a strategy game that will have a country full of kingdoms. I want to be able to store and read back in the information of the kingdomgs. I've looked at tutorials online but they just aren't specific to what I'm looking for. So basically:
-Where to store a text file which holds string values.
-The correct file path.
-And how to read from that text file, and check if it is empty.
You can store text data in string resource file:
https://developer.android.com/guide/topics/resources/string-resource.html
Also you can use asset to store text
https://developer.android.com/reference/android/content/res/AssetManager.html
And there is diffrenece:
Difference between /res and /assets directories

Android Preference file set name

I am currently following this tutorial Android Settings
my problem is it save the app settings as
com.packagename_preferences.xml
Is there any way I can set its file name? like this
mychoosennamehere_preferences.xml
or
somenamesiwanttouse.xml
Although you can rename the preference file but you should not do that. Actually, the exact location and name of this preference file is not documented, so I'd suggest you don't rely on some conventions when trying to access this file directly, since the location and name may be changed in future - SharedPreferences should be the only way to access this file.And your App will stop working.
Anyhow here is the way to rename your preference file
String fileName="mychoosennamehere_preferences";
File f=new File("/data/data/eywa.musicplayer/shared_prefs/"+"com.packagename_preferences"+".xml");
f.renameTo(new File("/data/data/eywa.musicplayer/shared_prefs/"+fileName+".xml"));
SharedPreferences mySharedPreferences=getSharedPreferences(fileName,Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.remove(PlayListName);
editor.putString(fileName, fileName);
editor.commit();
PlayListName=fileName;
You can change the name of the .xml file (right-click -> rename).
Then change the name in your code that points to the file.

Getting /data/data/my.app/files path programmatically?

In my app, I've just successfully downloaded a file and I opened it like this:
OutputStream output = openFileOutput("myfile.txt", Context.MODE_PRIVATE);
With a little browsing, I've found my file in /data/data/my.app.namespace/files/myfile.txt. I don't want to hardcode the path but later on in my app I'm going to want to access myfile.txt and I want to dynamically get that folder path. I've tried most of the folders in the Environment object like Environment.getDataDirectory().getAbsolutePath() but that only returns /data. Where can I look to get where private files go? Or is it safe to assume that this structure won't change? I don't like hardcoding it, but it would work...
you could do it by using context.getFilesDir() + "/filename.ext"

Android: Edit strings.xml inside java code

I was wondering if it was possible to edit a string saved in the strings.xml file. If you can then how do you do this. So far I cannot see that you can but I am new to android.
NO. You can't edit your res folder. The data what you have in your apk file is read only. You can't edit it on the go.

Add text file resources to R.java

I have text files named hierarchy1.txt, hierarchy1.1.txt and heirarchy1.1.1.txt in my android project->res->raw folder. These files have json objects. I m trying to read from these files but these are not getting added in my R.file. How do I resolve this?
You should use underscores instead of dots in your filenames.
If you rename your file to hierarchy1_1.txt you should be able to access it with:
context.getResources().openRawResource(R.raw.hierarchy1_1);

Categories

Resources