Im new to android development. Now im trying to use sqlite db. I created a database sqlite file using sqlite manager.
I have tried the following code, it works fine in emulator , but if I took release in device the app crashes ie,it shows an alert message The application CheckMobileForDatabase has stopped unexpectedly, my sdk version is 2.2(8)
private void StoreDatabase() {
File DbFile=new File("data/data/com.sqldemo/databases/idua1");
if(DbFile.exists())
{
System.out.println("file already exist ,No need to Create");
}
else
{
try
{
DbFile.createNewFile();
System.out.println("File Created successfully");
InputStream is = this.getAssets().open("idua1.sqlite");
FileOutputStream fos = new FileOutputStream(DbFile);
byte[] buffer = new byte[1024];
int length=0;
while ((length = is.read(buffer))>0)
{
fos.write(buffer, 0, length);
}
System.out.println("File succesfully placed on sdcard");
//Close the streams
fos.flush();
fos.close();
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Without your logcat, this is just shooting in the dark.
You could try checking the existence of the file another way:
And then run another method to copy the db if it doesn’t exist: