我想在后台服务的应用程序文件夹中创建目录。我使用getDir创建了一个目录。我正在发送服务上下文来创建DataManager的目录。此外,我已经确认我没有收到空的上下文。但我在“ctx.getDir("Application_Name", Context.MODE_PRIVATE)
”行遭遇崩溃。
private File systemDir;
private File dataDir;
public DataManager(Context ctx) {
try{
Log.e("xlogger", "DataManager Context : "+ctx);
systemDir = ctx.getDir("Application_Name", Context.MODE_PRIVATE );
dataDir = new File(systemDir, "Data/");
if(!dataDir.exists()){
dataDir.mkdir();
}
}catch (Exception e) {
Log.e("xlogger", "DataManager Exception : "+Log.getStackTraceString(e));
}
}
崩溃报告:
02-27 13:46:13.100: D/xlogger(4762): DataManager Context = com.MyApp.engine.MyService@4055ad38
02-27 13:46:13.110: E/xlogger(4762): DataManager Exception : java.lang.NullPointerException
02-27 13:46:13.110: E/xlogger(4762): at android.content.ContextWrapper.getDir(ContextWrapper.java:198)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.utils.DataManager.<init>(DataManager.java:30)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.communication.MyAppClient.getRulesXML(MyAppClient.java:867)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.engine.MyService$5.doInBackground(MyService.java:862)
02-27 13:46:13.110: E/xlogger(4762): at com.MyApp.engine.MyService$5.doInBackground(MyService.java:1)
02-27 13:46:13.110: E/xlogger(4762): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-27 13:46:13.110: E/xlogger(4762): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-27 13:46:13.110: E/xlogger(4762): at java.lang.Thread.run(Thread.java:1019)
高度赞赏。感谢。
答案 0 :(得分:0)
getDir()或openFileOutput():他们在数据/数据中创建文件//。这不适用于任何非root用户。所以,根据手机,或者我建议你尝试使用EMULATOR。
否则你可以使用getExternalFilesDirectory() - &gt;在SD卡上但路径是Android /数据//文件/并且它也是私有应用程序。 卸载该应用程序后,即使这些文件也会被删除: - )
如果有帮助,总是评价:-)