[Android SDK]Can't copy external database (13M

2019-01-26 23:00发布

问题:

I need a list of italian words for a game I'm developing but I can't actually make it copy my database from assets. I tried quitea lot of solutions I found on the website, such as:

  1. Using your own SQLite database in Android applications
  2. how to copy large database which occupies much memory from assets folder to my application?
  3. Load files bigger than 1M from assets folder

But I had no luck, it keeps on giving me this error on the line

os.write(buffer, 0, len);

but I can't understand why. Here's the function's code and the constants I'm using. A strange thins is that my database stops copying after 11.45MB, just 1 MB away from the goal.

Can someone help me solve this? Many thanks :)

回答1:

Use SQLiteAssetHelper, which has a debugged version of the package-the-database-with-the-app logic, so you do not need to mess with any of this yourself.



回答2:

First of all by default asset folder supports max size for db file is 1mb.

You need to divide your database into parts.

Download HJSplit and divide your database into small parts like 13MB = 13 parts each of 1MB.

demoDB.sqlitedb= 13MB then

demodb..sqlitedb.001
demodb..sqlitedb.002
demodb..sqlitedb.003
demodb..sqlitedb.004
...
...
demodb..sqlitedb.013

Then use the following code to merge your database.

private void copyDataBase() throws IOException {
        AssetManager am = mContext.getAssets();
        OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);
        byte[] b = new byte[1024];
        String[] files = am.list("");
        Arrays.sort(files);
        int r;
        for (int i = 1; i <= 9; i++) {
            InputStream is = am.open("demoDB.sqlitedb.00" + i);
            while ((r = is.read(b)) != -1) {
                os.write(b, 0, r);
            }
            Log.i("BABY_DATABASE_HELPER", "Copying the database (part " + i
                    + " of 9)");
            is.close();
        }
        os.close();
    }


回答3:

private void copyDataBase() throws IOException {
    AssetManager am = getAssets();
   OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);

    byte[] b = new byte[1024];
    String[] files = am.list("");
    Arrays.sort(files);
    int r;
    for (int i = 1; i <=21; i++) {
        InputStream is;

        if ( i < 10){
            System.out.println("coping file demoDB.sqlitedb.00"+i );
             is = am.open("demoDB.sqlitedb.00" + i);
        }else{
            System.out.println("coping file demoDB.sqlitedb.0"+i );
             is = am.open("demoDB.sqlitedb.0" + i);
       }
        while ((r = is.read(b)) != -1) {
            os.write(b, 0, r);
        }
        is.close();
    }
    os.close();
}


回答4:

I know this was answered but I ran into something like this while creating tests where I wanted to store a particular database with faults in it to test bad data. Problem was test database was not found at all in the assets. To even see it I had to do this:

InputStream is = mContext.createPackageContext("com.activities.tests", Context.CONTEXT_IGNORE_SECURITY).getAssets().open("mydb.db");

By ignoring the security you can see it and then take the inputstream and save it to an external directory.