对于我的应用程序,我需要从我自己的数据库中读取数据。我把我的分贝在“资产”文件夹,使用下面的代码复制到路径将数据从资产复制到应用程序的问题
DB_PATH = "/data/data/com.android.example/databases/"
,但该表没有得到created.And其引发异常而被迫关闭。
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
在上面的代码中,“buffer”没有获取数据。所以它不会将数据写入myOutput。我发现这个代码在网络和修改。任何机构都可以帮助我将资产文件夹中的文件读取到SD卡。我需要将数据从外部文件存储到SD卡中并从中读取。
你会得到什么例外?你可以附上日志吗? – jamapag