서버에서 데이터베이스를 다운로드 한 다음 내 응용 프로그램에서 사용하려고합니다. 내 초기 상황은 자산 폴더에 데이터베이스가 있고 사용자가 단추를 클릭 할 때 서버에서 전체 데이터베이스를 다운로드하려고하는 것입니다.데이터베이스를 다운로드하여 응용 프로그램에 통합하는 방법
나는 onUpgrade 메서드를 사용해야한다고 생각하지만 데이터베이스를 다운로드 한 다음 assetfolder에로드하는 방법을 모르겠습니다.
서버에서 데이터베이스를 다운로드 한 다음 내 응용 프로그램에서 사용하려고합니다. 내 초기 상황은 자산 폴더에 데이터베이스가 있고 사용자가 단추를 클릭 할 때 서버에서 전체 데이터베이스를 다운로드하려고하는 것입니다.데이터베이스를 다운로드하여 응용 프로그램에 통합하는 방법
나는 onUpgrade 메서드를 사용해야한다고 생각하지만 데이터베이스를 다운로드 한 다음 assetfolder에로드하는 방법을 모르겠습니다.
작은 문제로 문제를 해결해야합니다.
물론, 위의 모든 내용은 다중 스레드 환경에서 수행해야하며 Android 서비스를 고려해 볼 수도 있습니다.
이것은 서버에서 파일을 다운로드하는 예입니다. 아이디어를 얻는데 도움이 될 것입니다. 너무 일반적이지 않고 제 환경에서 작동합니다. 당신이 당신의 DB 파일을 다운로드 한 경우 이후
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;
}
, 당신은 단지의 SQLiteDatabase 객체를 만들 수 있습니다
final File dbFile = new File(filePath);
if (dbFile.exists()) {
this.localDatabase = SQLiteDatabase.openDatabase(filePath, null, SQLiteDatabase.OPEN_READWRITE);
this.localDatabase.setLocale(Locale.getDefault());
}
나는 아직도 당신이 또한 생각을 압축하는 주어야한다 의견에 충실을, 당신은을 감소 트래픽과 네트워크를 통해 소비 된 시간이 많지 않습니다.
자산 폴더 만 읽을 수 없습니다. – Yogendra