[Q] Populate database in android 4.4 using phonegap?

Search This thread

Delliany

New member
Jul 10, 2014
1
0
I can't populate database in android 4.4 using phonegap when I run my app the first time, but if I close the app and open it again this task works fine.

I have a 0000000000000001.db in assets and I want put this in Android 4.4 memory.

How can I deal with such Issue?

I'm using this code to populate the database in Android 4.4 memory

Code:
public void copyDatabase(){ 

// DB_NAME2 = "Databases.db"; 
//ASSETS = "0000000000000001.db"; 
//DB_PATH2 = "/data/data/com.example.testapp/app_webview/databases/"; 
//DB_PATH3 = "/data/data/com.example.testapp/app_webview/databases/file__0/"; 
//DB_NAME3 = "1"; 

String path = DB_PATH2 + DB_NAME2; 
String path2 = DB_PATH3 + DB_NAME3; 
File checkDatabase = new File(DB_PATH2); 
File checkDatabase2 = new File(DB_PATH3); 

if (!checkDatabase.exists()) 
{ 
checkDatabase.mkdir(); 
} 
if (!checkDatabase2.exists()) 
{ 
checkDatabase2.mkdir(); 
} 
try{ 

InputStream is = context.getAssets().open(DB_NAME2); 
OutputStream os = new FileOutputStream(path); 
InputStream is2 = context.getAssets().open(ASSETS); 
OutputStream os2 = new FileOutputStream(path2); 

byte[] buffer = new byte[10240]; 
int line; 

while ((line = is.read(buffer))>0) 
{ 
os.write(buffer, 0, line); 
} 

os.flush(); 
os.close(); 
is.close(); 

buffer = new byte[10240]; 
line = 0; 

while ((line = is2.read(buffer))>0) 
{ 
os2.write(buffer, 0, line); 
} 

os2.flush(); 
os2.close(); 
is2.close(); 
}catch(IOException e){ 
System.out.println("Problem "+e); 
} 
}