如何下载数据库并集成到应用程序中

时间:2014-02-26 11:41:00

标签: android sqlite

我正在尝试从服务器下载数据库,然后在我的应用中使用它。我最初的情况是我的资产文件夹中有一个数据库,当用户点击按钮时,我试图从服务器下载整个数据库。

我想我必须使用onUpgrade方法,但我不知道如何下载数据库然后使用它或将其加载到资产文件夹中。

1 个答案:

答案 0 :(得分:0)

您需要将问题归结为较小的问题。

  • 确保服务器端能够为您提供用于实际下载的网络服务。
  • 如果您的服务器会压缩您的sqlite数据库以便最大限度地减少流量,那将是理想的。
  • 在您的设备上下载数据库之后,如果采用这种方法,则必须解压缩。
  • 当你下载并解压缩数据库时,只需连接到sqlite数据库,Android就此提供api。

当然,以上所有需要在多线程环境中完成,您也可以考虑使用Android服务。

这是从服务器下载文件的一个示例,它应该可以帮助您获得一个想法,它不是太通用,它可以在我的环境中工作。

 private void handleSelectedItemDownload(final String downloadItem) {
    try {
        int bufferLength;
        long downloadedSize = 0;
        long downloadProgressBytes = 0;

        final byte[] buffer = new byte[12 * 163840];
        this.url = new URL(String.format(AppConstants.SERVICE_BASE_URL + AppConstants.MEDIA_IMAGE_REQUEST_URL_PATH + downloadItem));
        InputStream inputStream = this.getStream(downloadedSize);
        downloadedSize = this.chunkSize;

        if (downloadedSize > 0) {
            this.applicationFolder = downloadItem.equals("pictures") ? Utils.getThumbNailsFolder() : Utils.getApplicationFolder();
            final File mediaFolder = new File(this.applicationFolder);
            mediaFolder.mkdirs();

            final File file = new File(mediaFolder, String.format(File.separator + downloadItem + ".zip"));
            final FileOutputStream fileOutput = new FileOutputStream(file);
            this.actionStatusProgressBar.setProgress(0);

            final Message downloadStartMessage = new Message();
            downloadStartMessage.what = ACTION_STARTED;
            this.actionMessageHandler.sendMessage(downloadStartMessage);

            int progressBarMax = (int) (this.totalDownloadSize / 1024);
            this.actionStatusProgressBar.setMax(progressBarMax);


            while (chunkSize != 0) {
                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    fileOutput.write(buffer, 0, bufferLength);
                    downloadProgressBytes += bufferLength;

                    final Message currentProgressMessage = new Message();
                    currentProgressMessage.arg1 = (int) (downloadProgressBytes / 1024);
                    currentProgressMessage.what = ACTION_PROGRESS_VALUE;
                    this.actionMessageHandler.sendMessage(currentProgressMessage);
                }

                inputStream.close();
                this.urlConnection.disconnect();

                inputStream = this.getStream(downloadedSize);
                downloadedSize += chunkSize;
            }
            fileOutput.close();
        }

        this.urlConnection.disconnect();
        this.totalDownloadSize = 0;

    } catch (MalformedURLException e) {
        Log.e("DOWNLOAD_ERROR", e.getMessage() + e.toString());
        this.errorEncountered = true;
        this.actionType = IDataActionListener.ACTION_TYPE.ACTION_FAILED;
        this.actionMessageHandler.sendEmptyMessage(ERROR_ENCOUNTERED);
    } catch (IOException e) {
        Log.e("DOWNLOAD_ERROR", e.getMessage() + e.toString());
        this.errorEncountered = true;
        this.actionType = IDataActionListener.ACTION_TYPE.ACTION_FAILED;
        this.actionMessageHandler.sendEmptyMessage(ERROR_ENCOUNTERED);
    } catch (Exception e) {
        Log.e("DOWNLOAD_ERROR", e.getMessage() + e.toString());
        this.errorEncountered = true;
        this.actionType = IDataActionListener.ACTION_TYPE.ACTION_FAILED;
        this.actionMessageHandler.sendEmptyMessage(ERROR_ENCOUNTERED);
    }
}  

private InputStream getStream(final long downloadedSize) throws IOException {
    this.urlConnection = (HttpURLConnection) this.url.openConnection();

    this.urlConnection.setRequestMethod("GET");
    this.urlConnection.addRequestProperty("DOWNLOADED_SIZE", String.valueOf(downloadedSize));

    final InputStream inputStream = this.urlConnection.getInputStream();
    this.chunkSize = this.urlConnection.getContentLength();
    if (!Utils.stringIsNullOrEmpty(this.urlConnection.getHeaderField("TOTAL_SIZE"))) {
        if (this.totalDownloadSize == 0) {
            this.totalDownloadSize = Long.parseLong(this.urlConnection.getHeaderField("TOTAL_SIZE"));
        }
    }
    return inputStream;
}

之后,当您下载了db文件时,您只需创建一个SQLiteDatabase对象:

 final File dbFile = new File(filePath);

    if (dbFile.exists()) {
        this.localDatabase = SQLiteDatabase.openDatabase(filePath, null, SQLiteDatabase.OPEN_READWRITE);
        this.localDatabase.setLocale(Locale.getDefault());
    }

我仍然坚持认为你也应该考虑一下,你减少了流量和花在网络上的时间。