2014-02-26 3 views
0

서버에서 데이터베이스를 다운로드 한 다음 내 응용 프로그램에서 사용하려고합니다. 내 초기 상황은 자산 폴더에 데이터베이스가 있고 사용자가 단추를 클릭 할 때 서버에서 전체 데이터베이스를 다운로드하려고하는 것입니다.데이터베이스를 다운로드하여 응용 프로그램에 통합하는 방법

나는 onUpgrade 메서드를 사용해야한다고 생각하지만 데이터베이스를 다운로드 한 다음 assetfolder에로드하는 방법을 모르겠습니다.

+0

자산 폴더 만 읽을 수 없습니다. – Yogendra

답변

0

작은 문제로 문제를 해결해야합니다.

  • 서버 측에서 실제 다운로드 할 웹 서비스를 제공 할 수 있는지 확인하십시오.
  • 트래픽을 최소화하기 위해 서버가 sqlite 데이터베이스를 압축하는 것이 이상적입니다.
  • 장치에 db를 다운로드 한 후에는이 방법을 사용한 경우 압축을 풀어야합니다.
  • db가 다운로드되고 압축 해제되는 시점에 도달하면 sqlite db에 연결하는 문제 일 뿐이며 안드로이드는 이와 관련하여 API를 제공합니다.

물론, 위의 모든 내용은 다중 스레드 환경에서 수행해야하며 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()); 
    } 

나는 아직도 당신이 또한 생각을 압축하는 주어야한다 의견에 충실을, 당신은을 감소 트래픽과 네트워크를 통해 소비 된 시간이 많지 않습니다.

+0

예 ive 서버 및 everthing있어. 약 2MB 크기 때문에 데이터베이스를 압축 할 필요가 없습니다. 자습서 나 다른 것을 알려줄 수 있습니까? 제발 – schocka94

+0

코드 몇 장을 추가했습니다. 끝났습니다. 아이디어를 얻는 데 도움이 될 것입니다. 그렇지 않으면 몇 가지 추가 설명이 포함되어 있습니다. –

+0

더 쉬운 방법으로 전체 데이터베이스를 다운로드 할 수 있습니까? – schocka94

관련 문제