데이터베이스가 작기 때문에 압축하지는 않지만 이미지를 압축하여 (주로 묶음으로) 위치에 직접 압축을 푸십시오. 데이터베이스 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);
}
}
내부 및 외부 저장 공유 일반적인 공간 . – CommonsWare
내 최소 SDK는 2.2이고 일부 사용자는 내부 메모리에 공간이 없으므로 SD 카드에 앱과 DB를 모두 설치하려고합니다. –