Copying preloaded database, adding carriage returns

45 Views Asked by At

I have an app which comes with a preloaded database.

My temporary copy function is like so

private void copyDatabase() {

    AssetManager assetManager = context.getResources().getAssets();
    InputStream in = null;
    OutputStream out = null;
    try {
        in = assetManager.open(DATABASE_NAME);
        out = new FileOutputStream(DATABASE_FILE);
        byte[] buffer = new byte[1024];
        int read;

        while ((read = in.read(buffer)) > 0){
            out.write(buffer, 0, read);
        }
    } catch (IOException e) {
        Log.e("error", e.getMessage());
    } finally {
        if(in != null) {
            try {
                in.close();
            }catch (IOException e) {
                Log.e("error", e.getMessage());
            }
        }
        if(out != null) {
            try {
                out.close();
            }catch (IOException e) {
                Log.e("error", e.getMessage());
            }
        }
    }
}

However on Android 2.2 I keep getting an error "database disk image malformed". So I went ahead and copied it off the device, onto my computer. And sure enough, it wouldn't open. I did a hex compare on the two files and there are 10 instances where 1 byte is different. Hex 0D has been added 10 times in random spots in the malformed copy.
The copy routine works fine in 3.x+. I also have developed other apps with the same method and don't have an issue.

Any ideas?

1

There are 1 best solutions below

0
Kris On

Not sure what the issue was, but I created a new sqlite database, imported the data, and that seemed to work.