简体   繁体   中英

How can I create an external shared pref/config file for my Android application?

I am looking for a way to store configuration setting on an external file on the sd card. The app must look into this external file and then retrieve some kind of settings. At the moment i am trying to get it to look into a file and get a name. I know you can store things in shared preferences but they are internally accessed.

Anybody know an external way? Thanks

The idea is to have a simple text file or xml file on the sd card. So when a configuration needs changing it is done thru that file?

EDIT

File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard,"/Config.txt");
    StringBuilder text = new StringBuilder();
    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line="";
        int c;
        while ((c = br.read()) != -1) {
            line+=(char)c;
            if(String.valueOf(c) == ";" & line =="name;"){
                String name ="";
            }
        }
    }

I have attempted to read in a config.txt file and to separate each value with a ; But i cant seem to understand exactly how i am going to attach what comes after name; to the variable String name.

the config.txt file has the following:

name;fred somethingelse;test

The program should know when it has got to name and then set the name variable to fred??

Hi I had same kind of preferences reading in Blackberry with (Comma separated or) semicolon separated values. When same development came to android the developers (I was not on android at that time.) This is what they have done.

We had created a text file like this

"name";"value";"data_type"

For example

"application_runs";"1";"int"

Or

"trial_period_key";"01ab23cd";"string"

The data type is hardcoded so we can detect the datatypes. For variable names we also had hardcoded list of preferences. And values can be parsed accordingly. They have written public readable shared preference based on that txt files, to use default values, they have copied a "default.txt" in assets folder along with a copy in SD card.

The drawback of this procedure is

  1. You have to be careful about file name and the data written in that file (this is the reason why we had put a default values file in assets so the app doesn't crash)

  2. The text file on SD Card must be readable, you have to program it along with parsing.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM