java - Android copy error from asset -
i trying make application in android, using pre-created database. create data file , keep in asset
folder, when application run, copy /data/data/package name/databases/data
problem when run android 2.2
, copy correctly. when run android 2.3.3
over, doesn't copy database , not show errors. here code:
public class mainactivity extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); try { despath="/data/data/" + getpackagename()+"/databases/data"; file f=new file(despath); if(!f.exists()) { copydb( getbasecontext().getassets().open("data"),new fileoutputstream(despath)); } } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } public void copydb(inputstream inputstream, outputstream outputstream) throws ioexception { byte[] buffer=new byte[1024]; int length; while((length=inputstream.read(buffer))>0) { outputstream.write(buffer,0,length); } inputstream.close(); outputstream.close(); } }
Comments
Post a Comment