【问题标题】:How to copy db.zip from asset to SD card in android如何在android中将db.zip从资产复制到SD卡
【发布时间】:2014-06-13 11:24:50
【问题描述】:

我有一个 40MB 大小的 SQLite 数据库。我使用 SQLite Asset Helper 库来复制数据库并使用它。由于 APK 大小问题,我应该压缩我的数据库。该库工作得很好,但是它将数据库复制到内部内存中,并且 40MB 大小的数据库会导致未来的问题。 我想将我的数据库复制到 SD 中。

解决方案 1:使用 SQLite Asset Helper lib 将压缩后的 DB 复制到内存中,然后将 DB 移动到 SD。

解决方案2:直接将压缩后的DB复制到SD卡中。

所以请帮助我哪一个更好,我该怎么做。

【问题讨论】:

  • 内部和外部存储在大多数 Android 3.0+ 设备上共享公共空间。
  • 我的最小 SDK 是 2.2,有些用户内存没有空间,所以他们想在 SD 卡中安装应用程序和数据库。

标签: android database sqlite sd-card


【解决方案1】:

我的数据库很小,所以我不压缩它,但我压缩了一些图像(主要是为了捆绑它们)并将它们直接解压缩到一个位置。您应该能够为您的数据库 zip 文件调整以下代码。

我创建了一个在我的初始屏幕中触发的 AsyncTask,它将保持初始屏幕打开直到复制完成。

复制过程非常简单:

protected Void doInBackground(String... params) {
    final File dataBaseFile = new File(mDestinationFile);

    if (!dataBaseFile.exists()) {
        try {
            copyFromAssetsToSdcard();
            FileUtils.unzip(mContext.getAssets().open("images.zip"), Constants.IMAGE_CACHE_PATH + "/");
        } catch (IOException ioe) {
            Log.e(LOG_TAG, "Database can not be copied", ioe);
        }
    } else {
        Log.w(LOG_TAG, "Destination database already exists");
    }

    return null;
}

private void copyFromAssetsToSdcard() throws IOException {
    final BufferedInputStream inputStream = new BufferedInputStream(mContext.getAssets().open(mSourceFile));
    final OutputStream outputStream = new FileOutputStream(mTmpDestinationFile);
    copyStream(inputStream, outputStream);
    outputStream.flush();
    outputStream.close();
    inputStream.close();
    File tmpFile = new File(mTmpDestinationFile);
    if (tmpFile.renameTo(new File(mDestinationFile))) {
        Log.w(LOG_TAG, "Database file successfully copied!");
    } else {
        Log.w(LOG_TAG, "Database file couldn't be renamed!");
    }
}

而我的 FileUtils.unzip 方法只是解压到指定位置:

public static void unzip(InputStream zipInput, String location) throws IOException {
    try {
        File f = new File(location);
        if (!f.isDirectory()) {
            f.mkdirs();
        }
        ZipInputStream zin = new ZipInputStream(zipInput);
        try {
            ZipEntry ze = null;
            final byte[] buffer = new byte[BUFFER_SIZE];
            while ((ze = zin.getNextEntry()) != null) {
                String path = location + ze.getName();

                if (ze.isDirectory()) {
                    File unzipFile = new File(path);
                    if (!unzipFile.isDirectory()) {
                        unzipFile.mkdirs();
                    }
                } else {
                    FileOutputStream fout = new FileOutputStream(path, false);
                    try {
                        int length = zin.read(buffer);
                        while (length > 0) {
                            fout.write(buffer, 0, length);
                            length = zin.read(buffer);
                        }
                        zin.closeEntry();
                    } finally {
                        fout.close();
                    }
                }
            }
        } finally {
            zin.close();
        }
    } catch (Exception e) {
        Log.e(LOG_TAG, "Unzip exception", e);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    相关资源
    最近更新 更多