0

I have used pre-existing database in my application which placed in assets folder and I am using that pre-existing structured for storing data. this is working fine in all the Android devices.

But Somehow it is not working in Android P beta version. When I unzip the database and store into the internal memory and when closed that database object, database file has being corrupted so, after closed the database only "android-metadata" table is left in that database file other tables has been removed automatically.

Please advice.!! what will be the cause. Here is my code

public void open() throws SQLException {
        try {
            boolean isExist = mDatabaseHelper.checkDataBase();
            if (isExist == false) {
                mDatabase = mDatabaseHelper.getWritableDatabase();
                mDatabaseHelper.copyFromZipFile();
                if (mDatabase.isOpen()) {
                    mDatabase.close();
                }
            }
            mDatabase = mDatabaseHelper.getWritableDatabase();

        } catch (Exception e) {
            Logger.d(TAG, e.getMessage());
            e.printStackTrace();
        }
    }



/**
     * This method is used to close the dataHelper object.
     */
    public void close() {
        try {
            if (mDatabase != null && mDatabase.isOpen())
                mDatabase.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public boolean checkDataBase() {
        PACKAGE = mContext.getApplicationContext().getPackageName();
        DB_PATH = DATA + PACKAGE + "/databases/";
        Logger.d(TAG, DB_PATH);
        File f = new File(DB_PATH + mDatabaseName);
        return f.exists();
    }

    public void copyFromZipFile() throws IOException {
        InputStream is = mContext.getAssets().open("xyz.zip");
        // Path to the just created empty db
        PACKAGE = mContext.getApplicationContext().getPackageName();
        Logger.d(TAG, DB_PATH);
        File outFile = new File(DB_PATH, mDatabaseName);
        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFile.getAbsolutePath());
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
        try {
            while (zis.getNextEntry() != null) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int count;
                while ((count = zis.read(buffer)) != -1) {
                    baos.write(buffer, 0, count);
                }
                baos.writeTo(myOutput);
            }
        } finally {
            zis.close();
            myOutput.flush();
            myOutput.close();
            is.close();
        }
    }
akash
  • 13
  • 4
  • 2
    Please, provide your relevant code, sotfware, package, libraries versions, error codes and messages in the question. – sr9yar May 30 '18 at 06:11
  • I have updated my code in question. I am using 24.0.1 android libraries. there is no any error code but database file automatically corrupted and when I try to get data from database it throw exception that "no such table" is there – akash May 30 '18 at 06:45

0 Answers0