Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

join multiple files of database using java (android) results in corrupted database

I'm trying to join several files of a database in android. The file was split in the first place because of the limitation in 1MB of size of any files within the apk for android versions prior to 2.3. The file I'm trying to join together is a database, which is complicated in its own way to be placed in the right folder for SQLite to read it, and I'm using the following method:

private void copyDB() throws IOException {
        // list the files the db is splitted into
        int[] cc = { R.raw.cc1, R.raw.cc2, R.raw.cc3 };
        OutputStream out = new FileOutputStream(DB_PATH+DB_NAME);

        for (int k : cc) {
            InputStream in = mContext.getResources().openRawResource(k);
            int count;
            byte[] filebytes = new byte[1024];
            while((count = in.read(filebytes)) != -1) 
                out.write(filebytes, 0, count);
            in.close();
        }

        // clean up
        out.flush();
        out.close();
    }

As you can see, I'm using plain streams to join the files back together, and I'm almost positive that it works because it performs as expected with android 2.3. However, when I test on android 2.2 I'm getting a corrupt database (I don't have with me the exact message from the LogCat but the SQL error was 'Error: database disk image is malformed')

Is there anything that I can do to prevent the database from corrupting when I place it in the /data/data/package.name/database/ folder? Thank you for your time.

like image 819
Oscar Wahltinez Avatar asked May 10 '26 05:05

Oscar Wahltinez


1 Answers

This is not strictly the answer to your question, but may resolve your problem. When I encountered this problem, I believe I renamed the file to force Android to treat it as an uncompressable binary (gave it an extension of .mp3).

like image 153
kabuko Avatar answered May 11 '26 19:05

kabuko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!