I am writing this cause I had enough....
I could not take it anymore...
I read & read & read...& then, read a little more...just like before I wrote this question here!
I am trying to copy a ready made sqlite DB from assets to app DB folder.
I have tried all combinations of copying code, path code & more.
This is from SO:
/**
* http://stackoverflow.com/questions/10738623/copy-database-from-assets-folder-in-unrooted-device
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transfering bytestream.
* */
public static void copyDataBase(String dbname, Context mContxt) throws IOException {
myContext = mContxt;
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(dbname);
// Path to the just created empty db
// String outFileName = "/data/data/" + MainActivity.PACKAGE_NAME + "/databases/" + dbname;
String outFileName1 = "/data/data/" + MainActivity.PACKAGE_NAME; // + "/databases/" + dbname;
String outFileName = myContext.getFilesDir().getAbsolutePath().replace("files", "databases/"); // + "/Database/";
File newDir = null;
File dir = new File(myContext.getApplicationInfo().dataDir+"/databases");
dir.mkdirs();
if (!dir.isDirectory()) {
Log.v(TAG, "NOTaDIrectory");
newDir = new File(outFileName);
newDir.mkdir();
if(newDir.exists()) {
if (newDir.isDirectory()) {
Log.v("isDIrectory", newDir.toString());
}
}
} else {
Log.v(TAG, "NOTaDIrectory");
}
File outFileName1Dir = new File(outFileName1);
if (!outFileName1Dir.isDirectory()) {
newDir = new File(outFileName);
newDir.mkdir();
if(newDir.exists()) {
if (newDir.isDirectory()) {
Log.v("isDIrectory", newDir.toString());
}
}
} else {
Log.v(TAG, "ISaDIrectory");
}
Log.v(TAG, outFileName + dbname);
File file = new File(outFileName + dbname);
// File file = myContext.getFileStreamPath(dbname);
Log.v(TAG, "" + file.getPath() + " - " + file.getTotalSpace());
Log.v(TAG, outFileName + dbname);
File ff = new File(outFileName , dbname);
if (ff.exists()) {
Log.v(TAG, "FF -----------EXISTS");
}
String[] files = myContext.fileList();
for (String file1 : files) {
Log.v(TAG, file1.toString());
if (file1.equals(dbname)) {
Log.v(TAG, "FILE EXISTS");
}
}
if (file.exists()) {
Log.v(TAG, "FILE EXISTS");
if(file.isFile()) {
Log.v(TAG, "isFILE");
// return;
}
} else {
Log.v(TAG, "NO FILE");
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
Log.v(TAG, "Copying DB");
}
Log.v("FileExists", "" + file.createNewFile());
Log.v("FileWriteAble", "" + file.canWrite());
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
if (!file.canWrite()) {
Log.v("FileExists", "" + file.setWritable(true));
}
Log.v(TAG, "" + file.getPath() + " - " + file.getTotalSpace());
}
}
I know...its a bunch of code but some people would ask to see it....
JUST CANT SEEM TO WRAP MY HEAD AROUND THE PROBLEM.
Some basic code from MainActivity that call this function:
public class MainActivity extends FragmentActivity {
Context myContext;
String DB_PATH = null;
String DB_NAME = null;
public static ContextWrapper cw = null;
public static String PACKAGE_NAME;
After that, the call and some other defs: **Most of the commented lines were used in different attempts to trying and accomplish this task...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myContext = getApplicationContext();
// ContextWrapper
cw = new ContextWrapper(getApplicationContext());
// DB_PATH =cw.getFilesDir().getAbsolutePath()+ "/Database/";
DB_NAME = "dates1.sqlite";
// DB_PATH = cw.getFilesDir().getAbsolutePath().replace("files", "Databases/"); // + "/Database/";
/* DB_PATH = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases";
Log.v(TAG, DB_PATH);
*/
// get package name from anywhere
PACKAGE_NAME = getApplicationContext().getPackageName();
/* DbHelperNew dbNew = new DbHelperNew(cw, DB_NAME, null, 1);
Boolean T = dbNew.checkDataBase();
Log.v(TAG, T.toString());
*/
String[] files = this.fileList();
for (String file : files) {
Log.v(TAG, file.toString());
if (file.equals(DB_NAME)) {
Log.v(TAG, "FILE EXISTS");
}
}
try {
DbUtils.copyDataBase(DB_NAME, myContext);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// DbUtils.copyDataBase(DB_NAME, DB_PATH, myContext);
/* File src = new File("file:///android_asset/" + DB_NAME);
File dest = new File(DB_PATH + DB_NAME);
DbUtils.movedb(src, dest);
*/
for (String file : files) {
Log.v(TAG + "SECOND", file.toString());
if (file.equals(DB_NAME)) {
Log.v(TAG, "FILE EXISTS");
}
}
/* END */
_dbHelper = new DbHelper(getApplicationContext());
db = _dbHelper.getReadableDatabase();
I do apologise for the messy code...just to show the different attempts...
ERROR msgs:
09-23 09:21:45.609: V/DbUtils(12851): Copying DB
09-23 09:21:45.621: W/System.err(12851): java.io.IOException: open failed: ENOTDIR (Not a directory)
09-23 09:21:45.628: W/System.err(12851): at java.io.File.createNewFile(File.java:940)
09-23 09:21:45.640: W/System.err(12851): Caused by: libcore.io.ErrnoException: open failed: ENOTDIR (Not a directory)
09-23 09:21:45.699: E/SQLiteOpenHelper(12851): Couldn't open dates1.sqlite for writing (will try read-only):
09-23 09:21:45.699: E/SQLiteOpenHelper(12851): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
09-23 09:21:45.707: E/SQLiteLog(12851): (14) cannot open file at line 30174 of [00bb9c9ce4]
09-23 09:21:45.707: E/SQLiteLog(12851): (14) os_unix.c:30174: (20) open(/data/data/com.ndroidians.listfrag/databases/dates1.sqlite) -
09-23 09:21:45.710: E/SQLiteDatabase(12851): Failed to open database '/data/data/com.ndroidians.listfrag/databases/dates1.sqlite'.
09-23 09:21:45.710: E/SQLiteDatabase(12851): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
In order to solve the different error issues, I did:
- Try different directories, inc.: code for different OS vers, different FOLDER/ PACKAGE accessing code, creating the Dir.
- Changed file extension from */db to *.sqlite
- Changed file permissions to 777 in assets folder
- Tried different copy functions
- Tried different Phones
I might have forgotten something along the way but it has been 2 long days that I am cracking my head over this simple (seemingly) task...
HELP....
EDIT
Some more info:
I have trid to chown as suggested...still got the same error:
Emulator (this time)
>= 4.2 API17
New database is being copied to device! dates1.sqlite
New database has been copied to device!
chown 10054:10054 /data/data/com.ndroidians.listfrag/databases/dates1.sqlite
AFTER PROCESS
(14) cannot open file at line 30176 of [00bb9c9ce4]
(14) os_unix.c:30176: (20) open(/data/data/com.ndroidians.listfrag/databases/dates1.sqlite) -
Failed to open database '/data/data/com.ndroidians.listfrag/databases/dates1.sqlite'.
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
The funny thing about it is that on Emulator it shows that the databases folder is not a folder but kind of a file...
I attach a picture....
On the other hand on a device its show Data as correct size in App Info.
I attach a screen capture....
This is a riddle....anyone???