开发者

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, 开发者_C百科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.


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).


I had the same problem, however I realized that it was the way I was splitting my db incorrectly. Originally I split it this way;

split largeFile.db -b 1M largeFile

i was forgetting the .db at the end of the second largeFile hence,

split largeFile.db -b 1M largeFile.db

solved my problem. I then moved all my largeFile.dbaa, largeFile.dbab, ... in the assets (I suppose raw your case works as well) folder and renamed them to largeFileA, largeFileB,...

Maybe this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜