0

I am trying to copy database from assets folder in my application. Code works perfectly on Android 2.1/2.3 but while i execute same code on Android 2.2 device its throws following exception :

09-03 15:54:09.762: I/System.out(610): File Created successfully
09-03 15:54:09.762: D/asset(610): Data exceeds UNCOMPRESS_DATA_MAX (4640768 vs 1048576)
09-03 15:54:09.762: W/System.err(610): java.io.IOException
09-03 15:54:09.782: W/System.err(610):  at android.content.res.AssetManager.readAsset(Native Method)
09-03 15:54:09.782: W/System.err(610):  at android.content.res.AssetManager.access$700(AssetManager.java:36)
09-03 15:54:09.862: D/dalvikvm(610): GC_FOR_MALLOC freed 2198 objects / 260688 bytes in 71ms
09-03 15:54:09.862: W/System.err(610):  at android.content.res.AssetManager$AssetInputStream.read(AssetManager.java:571)
09-03 15:54:09.862: W/System.err(610):  at com.emobi.metro.Help.createDatabase(Help.java:39)
09-03 15:54:09.862: W/System.err(610):  at com.emobi.metro.Help.<init>(Help.java:22)
09-03 15:54:09.862: W/System.err(610):  at com.emobi.metro.Fair.onCreate(Fair.java:41)
09-03 15:54:09.862: W/System.err(610):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-03 15:54:09.862: W/System.err(610):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-03 15:54:09.862: W/System.err(610):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-03 15:54:09.862: W/System.err(610):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-03 15:54:09.862: W/System.err(610):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-03 15:54:09.862: W/System.err(610):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 15:54:09.862: W/System.err(610):  at android.os.Looper.loop(Looper.java:123)
09-03 15:54:09.862: W/System.err(610):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-03 15:54:09.862: W/System.err(610):  at java.lang.reflect.Method.invokeNative(Native Method)
09-03 15:54:09.862: W/System.err(610):  at java.lang.reflect.Method.invoke(Method.java:521)
09-03 15:54:09.862: W/System.err(610):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-03 15:54:09.862: W/System.err(610):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-03 15:54:09.862: W/System.err(610):  at dalvik.system.NativeStart.main(Native Method)

Following function is used to copy DB from assets:

private void createDatabase() {
    File DbFile = new File(
            "data/data/com.emobi.metro/databases/mymetro");
    if (DbFile.exists()) {
        System.out.println("file already exist ,No need to Create");
    } else {
        try {
            DbFile.createNewFile();
            System.out.println("File Created successfully");
            InputStream is = context.getAssets().open(databasename);
            FileOutputStream fos = new FileOutputStream(DbFile);
            byte[] buffer = new byte[1024];
            int length = 0;
            while ((length = is.read(buffer)) > 0) {
                fos.write(buffer, 0, length);
            }
            System.out.println("File succesfully placed on sdcard");
            // Close the streams
            fos.flush();
            fos.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Yes i tried that too but then i get following Exception 09-03 16:14:26.652: E/AndroidRuntime(2165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.emobi.metro/com.emobi.metro.Fair}: android.database.sqlite.SQLiteException: no such table: fair: , while compiling: select stations from fair

Which is available in Database.

Krishnakant Dalal
  • 3,568
  • 7
  • 34
  • 62

3 Answers3

0

There is many possible solutions to be found on the web, here is a good discussion on stackoverflow: I get this error: Data exceeds UNCOMPRESS_DATA_MAX on android 2.2 but not on 2.3

It seems to be related to android compressing files above 1 mb.

Community
  • 1
  • 1
Warpzit
  • 27,966
  • 19
  • 103
  • 155
0

Your data exceeds size above 1 mb In this case just rename your database name to dbname.mp3

private void copyDataBase() throws IOException {

        try {
            // Open your local db as the input stream
            InputStream myInput = context.getAssets().open("databases/" + DB_NAME + ".mp3");

            // Path to the just created empty db
            String outFileName = DB_PATH + DB_NAME;

            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);
            }

            // Close the streams
            myOutput.flush();
            myOutput.close();
            myInput.close();
        } catch (Exception e) {

            Log.e("Error in copy DB", e.toString());

        }

    }
bindal
  • 1,940
  • 1
  • 20
  • 29
0

Really zip the database with winzip and put the zip in asset folder, at run time unzip and copy it to database folder.

Ali
  • 21,572
  • 15
  • 83
  • 95