【发布时间】:2016-05-25 18:50:20
【问题描述】:
public void copyDbToInternalFolder() throws IOException {
String DB_PATH = Environment.getDataDirectory().getPath()+getPackageName()+"/Databases/"; // Don't worry I know this line doesn't work, however, nothing was working
String DB_NAME = "Client_Responses.db";
InputStream myInput = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS,"Client_Responses.db"); //needs to get database file from Downloads folder
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
我已经编写了几个相关的应用程序,它们在设备下载文件夹中创建 sqlite 数据库文件。我需要坚持这种方法,才能一直支持到第一代平板电脑。本质上是 API 8。
现在,管理员需要从 Downloads 文件夹中导入找到的文件,并将它们导入到自己的数据库文件夹中。
上面的代码不起作用,空指针异常和不正确的文件名/路径名会产生错误,应用程序将强制退出,我已经尝试了一切。我的想法现在陷入了瓶颈。
我一直在为此苦苦挣扎,欢迎提供意见。
注意:我很可能会在不同的类文件中复制代码,每个类文件都复制不同的数据库名称。
【问题讨论】:
-
根据stackoverflow.com/questions/19409137/…,我一直在研究使用 Apache commons 下的 fileutils 的替代方案