zip从资产复制到android中的SD卡

zip从资产复制到android中的SD卡

本文介绍了如何将db.zip从资产复制到android中的SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个40MB大小的SQLite数据库。我使用SQLite Asset Helper库复制数据库并使用它。由于APK大小问题,我应该压缩我的数据库。该lib可以很好地运行,但是它将数据库复制到内部存储器中,并且40MB的数据库大小会导致将来出现问题。
i想要将我的数据库复制到SD。

i have a SQLite DB with 40MB size. i use SQLite Asset Helper library to copy DB and use it. because of APK size issue i should to zip my DB. the lib works great, but it copy DB into internal memory and 40MB size of DB causes future problems.i want to copy my DB into SD.

解决方案1:将带有SQLite Asset Helper lib的压缩数据库复制到内部存储器中,然后将数据库移至SD。

solution 1: copy zipped DB with SQLite Asset Helper lib into internal memory and then move DB to SD.

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

Solution 2: copy zipped DB into SD card directly.

所以请帮助我,哪一个更好,以及我如何可以做到。

So please help me which one is better and how i can do it.

推荐答案

我的数据库很小,所以我不压缩它,但是我压缩了一些图像(主要是(将它们捆在一起),然后直接将其打开包装。您应该能够为数据库zip文件改编以下代码。

我创建了一个AsyncTask,该脚本在初始屏幕中触发,并保持初始屏幕打开直到复制完成。

I created an AsyncTask that is triggered in my splash screen and will keep the splash screen open until the copy was finished.

复制过程非常简单:

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方法只是解压缩到指定的文件中位置:

And my FileUtils.unzip method just unpacks into the specified location:

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

这篇关于如何将db.zip从资产复制到android中的SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:43