1

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.

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

Solution 2: copy zipped DB into SD card directly.

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

hojjat reyhane
  • 648
  • 1
  • 8
  • 25

1 Answers1

1

My database is small so I don't zip it but I zip some images (mainly to bundle them) and unpack them directly to a location. You should be able to adapt the below code for your database zip file.

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

The copy process is pretty simple:

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

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);
    }
}
WarrenFaith
  • 57,492
  • 25
  • 134
  • 150