I want to copy my sample database from my asset folder to the /data/data/packagename/databases/ directory to use it later in the application.
I have viewed many tutorial and other solution like here( Copy Database from assets folder in unrooted device) and here (copy database from assets to databases folder) but it not work for me. I have no error but the database that has been copied is empty. Is there something wrong with my coding?
Here is my coding for copying the database.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_dictionary_type);
File dbfile=new File("data/data/com.example.myidictionary/databases","DefinitionDB");
if (!dbfile.exists()) {
try {
copyDatabase(dbfile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Sample data is being copied", Toast.LENGTH_LONG).show();
}
else
Toast.makeText(getApplicationContext(), "Database Exist", Toast.LENGTH_LONG).show();
}
private void copyDatabase(File dbFile) throws IOException
{
InputStream is = this.getAssets().open("DefinitionDB");
OutputStream os = new FileOutputStream(dbFile);
byte[] buffer = new byte[1024];
while (is.read(buffer) > 0)
{
os.write(buffer);
}
os.flush();
os.close();
is.close();
}