0

我在我的应用程序中使用了预先存在的数据库,该数据库放置在资产文件夹中,并且我正在使用预先存在的结构来存储数据。这在所有 Android 设备上都运行良好。

但不知何故,它不适用于 Android P 测试版。当我解压缩数据库并将其存储到内部存储器中并关闭该数据库对象时,数据库文件已损坏,因此,关闭数据库后,该数据库文件中只剩下“android-metadata”表,其他表已被自动删除。

请指教。!!会是什么原因。这是我的代码

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

0 回答 0