2014-06-13 7 views
1

저는 40MB 크기의 SQLite DB를 가지고 있습니다. SQLite Asset Helper 라이브러리를 사용하여 DB를 복사하고 사용합니다. APK 크기 문제로 인해 DB를 압축해야합니다. lib는 훌륭하게 작동하지만 DB를 내부 메모리에 복사하고 DB의 크기가 40MB이므로 향후 문제가 발생합니다. DB를 SD에 복사하려고합니다.android에서 db.zip을 자산에서 SD 카드로 복사하는 방법

솔루션 1 : SQLite Asset Helper 라이브러리가있는 Zip DB를 내부 메모리에 복사 한 다음 DB를 SD로 이동합니다.

해결 방법 2 : 압축 된 DB를 SD 카드에 직접 복사하십시오.

그래서 어느 것이 더 좋고 어떻게 할 수 있는지 알려주세요.

+0

내부 및 외부 저장 공유 일반적인 공간 . – CommonsWare

+0

내 최소 SDK는 2.2이고 일부 사용자는 내부 메모리에 공간이 없으므로 SD 카드에 앱과 DB를 모두 설치하려고합니다. –

답변

1

데이터베이스가 작기 때문에 압축하지는 않지만 이미지를 압축하여 (주로 묶음으로) 위치에 직접 압축을 푸십시오. 데이터베이스 zip 파일에 대해 아래 코드를 적용 할 수 있어야합니다.

스플래시 화면에서 트리거되는 AsyncTask를 만들었고 복사본이 완료 될 때까지 스플래시 화면을 열어 두었습니다.

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 방법은 지정된 위치에 압축을 풉니 다 :

복사 프로세스는 매우 간단하다 대부분의 안드로이드 3.0 이상 기기에

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

나는 그것을 시도 할 것이다. 감사. –