https://stackoverflow.com/a/9172615/2653579
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
@Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
String ipAddress = settings.getString("IPAddress", "");
int port = settings.getInt("port", 0);
int title = settings.getString("Title", "");
}
我发现了从配置文件中读取值的代码,但我无法让它从 SD 卡中读取文件。我试过这样:
static File GPSCfgFile = new File(Environment.getExternalStorageDirectory()
+ "/Config/GPSLogService.cfg");
public static final String PREFS_NAME = String.valueOf(GPSCfgFile);
当我开始服务时,它意外停止。这是在 logcat 中:
FATAL EXCEPTION: main
GPSLogService.cfg.xml contains a path separator
我怎样才能让它从 SD 卡中读取该文件?