【问题标题】:Android App External ConfigurationAndroid 应用外部配置
【发布时间】:2013-06-05 19:22:44
【问题描述】:
我要的场景如下:
- “通用”的 Android 应用程序 (apk),即我不想使用不同的资源重新编译
- APK 将“预装”在设备上,因此不会成为“市场”应用
- 应用程序需要一个自定义配置文件,该文件使用文本等对其进行自定义,每次安装都可能不同。 (配置文件需要在同一个位置同名)
- 在应用启动时读取配置文件并根据配置文件中的数据配置应用
本质上,这将是一种配置应用程序的方法,以便它根据配置文件呈现特定的外观,而无需重新编译。能够加载自定义图片文件、文本数据等。
问题在于配置文件需要轻松更新并“复制”到没有 SD 卡的非 root 设备。所以我需要访问一个非应用程序特定的位置,该位置可以通过 USB 连接轻松访问,并且 APK 在运行时可以访问。似乎 SharedPreferences 和 Android 文件 IO 仅限于 /data/data/pkg/... 或外部存储下的私有应用程序目录。任何想法将不胜感激。谢谢。
【问题讨论】:
标签:
android
configuration
external
【解决方案1】:
只是想我会更新至少部分答案。至少我的一些问题与在我的 Razr Maxx 上的调试模式下进行测试有关。当我通过 USB 调试连接时,创建新文件的调用失败如下:
06-06 10:04:30.512:W/System.err(2583):java.io.IOException:打开失败:EACCES(权限被拒绝)
06-06 10:04:30.512: W/System.err(2583): at java.io.File.createNewFile(File.java:940)
当我在我的设备或模拟器中独立运行该应用程序时,一切都会按预期运行。不确定这是否与 Razr Maxx 或其他问题特别相关?
我的工作代码是(来自: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);
}