I have a SQLite Database and I would like to use in my Android App.
First I copy my database from Assets to /data/data//databases/ using:
private void copyDBFromAsssetsToDataFolder()
{
try
{
String destPath = "/data/data/" + getPackageName() + "/databases/dbname.db";
File f = new File(destPath);
if(!f.exists())
{
Log.v("TAG","File Not Exist");
InputStream in = getAssets().open("dbname.db");
OutputStream out = new FileOutputStream(destPath);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0)
{
out.write(buffer, 0, length);
}
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
Log.v("TAG","ioexeption");
e.printStackTrace();
}}
All my queries works fine in simulator but in the device I'm receiving:
Error opening directory: /data/data//databases/
Here, I'm reading people with the same problems and the solution is always assign permissions to this directory or copying the database to an external storage. But in production, the user does not have to have SD Card or you cant force to the user to assign permissions to the database (or other) directory.
What is the best solution to use databases already created?
.
stackoverflow.comm
No comments:
Post a Comment