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

南楼画角 提交于 2019-12-03 08:59:14

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!