Copy Database in assets to data/database work in emulator but not work in device -
i have code: working in emulator fail in devices. want copy database stored in assets data/data/.... when load app apears "error file" (filenotfoundexception e) -> error
public void copiardb() { try { string destpath = "/data/data/" + getpackagename() + "/databases/mibd"; file f = new file(destpath); if (!f.exists()) { copydb(getbasecontext().getassets().open("datosejer"), new fileoutputstream(destpath)); } } catch (filenotfoundexception e) { e.printstacktrace(); toast.maketext(mainactivity.this, "error file", toast.length_short) .show(); } catch (ioexception e) { e.printstacktrace(); toast.maketext(mainactivity.this, "error io", toast.length_short) .show(); } adaptadorbd db = new adaptadorbd(this); // ---obtener todos los contactos--- db.abrir(); cursor c = db.obtenertodoslosdatosejercicio(); if (c.movetofirst()) { { // displaycontact(c); } while (c.movetonext()); } db.cerrar(); } public void copydb(inputstream inputstream, outputstream outputstream) throws ioexception { // ---copy 1k bytes @ time--- byte[] buffer = new byte[1024]; int length; while ((length = inputstream.read(buffer)) > 0) { outputstream.write(buffer, 0, length); } inputstream.close(); outputstream.close(); toast.maketext(mainactivity.this, "se ha copiado la base de datos", toast.length_short).show(); }
i run in emulator , work perfectly, in devices dont work, os of emulator 4.3 , os of devices 4.0.4(rooted),4.0.4 , 4.3.
one line of db is:
14 11107 ovalo con mancuernas 11 hombros 3 12 10 no hay datos utiliza inicialmente las mancuernas con menor peso que encuentres en tu gimnasio para que aprendas el movimiento y dominar el peso. trabajarás la región anterior del hombro. img11107_1.png img11107_2.png img11107_3.png
updated:
i put solution in next comment.
finanlly found de solution:
public void copydb() { string destpath = "/data/data/" + getpackagename() + "/databases/mibd"; file f2 = new file(destpath); try { if (!f2.exists()) { //create empty file inputstream istream = getbasecontext().getassets().open( "datosejer"); adaptadorbd db = new adaptadorbd(getapplicationcontext()); db.open(); db.close(); outputstream ostream = new fileoutputstream(destpath); //open file created , write data. db.open(); byte[] buffer = new byte[1024]; int length; while ((length = istream.read(buffer)) > 0) { ostream.write(buffer, 0, length); } istream.close(); ostream.close(); db.close(); } } catch (ioexception e1) { e1.printstacktrace(); toast.maketext(mainactivity.this, "error.", toast.length_short).show(); } }
now work in device
Comments
Post a Comment