csheets
csheets

Reputation: 433

Android App External Configuration

The scenario I am going for is as follows:

  1. Android application (apk) that is "generic", i.e. I do not want to recompile with different resources
  2. APK will be "pre-installed" on devices and so will not be a "market" app
  3. The application needs a custom configuration file that customizes it with text, etc. that can be different for each installation. (config file needs to be in same location with same name)
  4. On app start-up the config file gets read and configures app according to the data in the config file

In essence, this would be a way to provision the application so that it takes on a specific look and feel based on the config file without having to recompile. Be able to load custom image files, text data, etc.

The problem is that the config file needs to be easily updated and "copied" to a non-rooted device without a SD card. So I need access to the a non-app specific location that is easily accessible via a USB connection and that the APK has access to at run-time. It seems that SharedPreferences and Android file IO is limited to the private app directory under /data/data/pkg/... or external storage. Any ideas would be greatly appreciated. Thanks.

Upvotes: 0

Views: 2041

Answers (1)

csheets
csheets

Reputation: 433

Just thought I would update with at least a partial answer. At least some of my issue has to do with testing in debug mode on my Razr Maxx. When I am connected via USB debugging then the call to create a new file fails as follows:

06-06 10:04:30.512: W/System.err(2583): java.io.IOException: open failed: EACCES (Permission denied) 06-06 10:04:30.512: W/System.err(2583): at java.io.File.createNewFile(File.java:940)

When I am running the app standalone on my device or in an emulator then it all works as expected. Not sure if this is related specifically to Razr Maxx or some other issue?

My code that is working is (from: Write a file in external storage in Android) :

private void writeToSDFile(){

    // Find the root of the external storage.
    // See http://developer.android.com/guide/topics/data/data-  storage.html#filesExternal

    File root = android.os.Environment.getExternalStorageDirectory(); 
    mStatusTV.append("\nExternal file system root: "+root);

    // See https://stackoverflow.com/questions/3551821/android-write-to-sd-card-folder

    File dir = new File (root.getAbsolutePath() + "/download");
    dir.mkdirs();
    File file = new File(dir, "myData.txt");


    try {
        file.createNewFile();

        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.println("Hi , How are you");
        pw.println("Hello");
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
        mStatusTV.append("Write File 1: " + e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        mStatusTV.append("Write File 2: " + e.getMessage());
    }   
    mStatusTV.append("\n\nFile written to "+file);
}

Upvotes: 1

Related Questions