我正在尝试在 android 中创建一个移动字典应用程序。作为应用程序的一部分,我试图将压缩的数据库文件从资产文件夹复制到 /data/data/package_name/app_database/file__0/。我正在使用下面的代码来执行此操作。以下代码无法解压缩并将数据库复制到所需位置,但在应用程序第二次启动时工作正常。
public class MainActivity extends DroidGap {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try
{
String pName = this.getClass().getPackage().getName();
this.copy("0000000000000001.zip","/data/data/"+pName+"/app_database/file__0/");
}
catch (IOException e)
{
e.printStackTrace();
}
super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html", 10000);
}
void copy(String file, String folder) throws IOException {
File CheckDirectory;
CheckDirectory = new File(folder);
if (!CheckDirectory.exists())
{
CheckDirectory.mkdir();
}
InputStream in = getApplicationContext().getAssets().open(file);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));
try {
ZipEntry ze;
while ((ze = zis.getNextEntry()) != null) {
String outputfilename = ze.getName();
OutputStream out = new FileOutputStream(folder+outputfilename);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
byte[] bytes = baos.toByteArray();
out.write(bytes);
baos.reset();
}
out.flush();
out.close();
}
}
finally {
zis.close();
in.close();
}
}
}
提前致谢。