2011-07-07 45 views
0

对于我的应用程序,我需要从我自己的数据库中读取数据。我把我的分贝在“资产”文件夹,使用下面的代码复制到路径将数据从资产复制到应用程序的问题

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卡中并从中读取。

+0

你会得到什么例外?你可以附上日志吗? – jamapag

回答

0

你必须把数据不在ASSETS文件夹,但assets - 请仔细检查!

1

检查您的数据库文件大小,“assets”文件夹中的文件的大小约为1.2 Mb。 解决它的一个办法是使用Unix“split”命令,在res/raw文件夹中添加拆分文件,然后通过稍微修改代码来将它们拼接在一起。 阅读此博客(http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/)以获取更多信息。

相关问题