【发布时间】:2017-05-08 12:08:44
【问题描述】:
我正在使用带有 Android 的 ProgramAB,这需要我通过在其中创建 /bots/ 目录将 aiml 文件从资产文件夹复制到外部存储。这是我从 this 教程中获取的代码,该教程教授了 android 中的聊天机器人(此代码中调用的其他函数与目录复制无关)。
我尝试了其他解决方案,但没有任何效果。
//checking SD card availablility
boolean a = isSDCARDAvailable();
//receiving the assets from the app directory
AssetManager assets = getResources().getAssets();
File jayDir = new File(Environment.getExternalStorageDirectory().toString() + "/hari/bots/Hari");
boolean b = jayDir.mkdirs();
if (jayDir.exists()) {
//Reading the file
try {
for (String dir : assets.list("Hari")) {
File subdir = new File(jayDir.getPath() + "/" + dir);
boolean subdir_check = subdir.mkdirs();
for (String file : assets.list("Hari/" + dir)) {
File f = new File(jayDir.getPath() + "/" + dir + "/" + file);
if (f.exists()) {
continue;
}
InputStream in = null;
OutputStream out = null;
in = assets.open("Hari/" + dir + "/" + file);
out = new FileOutputStream(jayDir.getPath() + "/" + dir + "/" + file);
//copy file from assets to the mobile's SD card or any secondary memory
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
//get the working directory
MagicStrings.root_path = Environment.getExternalStorageDirectory().toString() + "/hari";
System.out.println("Working Directory = " + MagicStrings.root_path);
AIMLProcessor.extension = new PCAIMLProcessorExtension();
//Assign the AIML files to bot for processing
bot = new Bot("Hari", MagicStrings.root_path, "chat");
chat = new Chat(bot);
String[] args = null;
mainFunction(args);
【问题讨论】:
-
boolean b = jayDir.mkdirs();。如果目录尚不存在,您应该只调用 mkdirs。如果 mkdirs 返回 false,您应该向用户显示敬酒。并返回。现在你盲目地继续。请修改您的代码。 -
@greenapps 我真的是 android 新手,我什至无法检查复制是否真的发生了。代码是否因为您陈述的问题而没有执行任务?
-
您应该告诉我们哪些有效,哪些无效。使用日志语句和 toast 看看会发生什么。做一些基本的调试。
-
实际上我正在使用 Visual Studio 模拟器,因此调试变得更像是一种开销,因为 adb 链接问题等等。将尽快定制代码以供公众参考。 :)
标签: java android android-assets