开发者

Copying database in assets to Android's internal databases fails at first attempt in the emulator

I have a database (1.69 MB) in assets to need copy to internal databases folder. My problem is that for the first try db is copied but not with exact tables. It becomes 3072 (3kB) size in the emulator file explorer.

When I look in that db with sqlite explorer I can't see my tables. The only table exists in db is android_metadata table with one column of my locale info.

But if I clear that db from databases and re-run the application, it seems working for this time. Why does it fail for the first try? How can I be sure it won't happen in real devices? Is th开发者_JAVA百科at a bug with the emulator?


finally i've come up the conclusion with the help of Naresh (thank you). Here the short summary: at the first run i was trying to copy my 1.6mb data.db file from assets to /data.../databases folder which one never existed. So

OutputStream os = new FileOutputStream(destPath);

line gave error. But after that line my dbhelper instance called getreadabledatabase which created the databases folder under specified path. Android put a db with same name but no useful data in it. In my copyDatabase method i updated it as follows:

InputStream is = getBaseContext().getAssets().open(assetsDB);

            //when there is no databases folder fileoutputstream gives error,
            //we have to make sure databases folder exists
            DbAdapter temp = new DbAdapter(getApplicationContext());
            temp.open();        //gets readable database: creates databases folder containing DB_NAME db
            temp.close();       //since we don use this temp, we close

            //this wont give error: because path is now exists (databases folder exists)
            OutputStream os = new FileOutputStream(destPath);

            //copying 1K bytes at a time
            byte[] buff = new byte[1024];

this is the solution i've come up with. By the way, former sdk's of 2.3 assets database size should be less than 1mb. This is anohter issue and found the solution here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜