I would like to import a database which is larger than 10 MB. I have not found a solution on the internet... Furthermore, I would like insert this DB in the final APK. Do you have any ideas ?
Sorry for my English. I am French
Basically what I did once for similar database was the thing described here. You create the database and put it in your assets
folder. Then have SQLiteOpenHelper
that copies it in the place where the application databases should reside (i.e. the private application storage).
However with as big file I needed to split the database file in several files as described here, because files larger than 1MB can not be read from the assets
folder. However you should be aware that your apk will become significantly large, of course.
I think you would either have to:
can use ZipInputStream to handle the large files.use zip util zip your files in project's asset folder
InputStream dataSource = getAssets().open("assets.zip");
unzip(dataSource, MainTab1Activity.defaultDBPath);
public static void unzip(InputStream zipFileName, String outputDirectory) {
try {
ZipInputStream in = new ZipInputStream(zipFileName);
// 获取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
// 当getNextEntry方法的返回值为null,则代表ZipInputStream中没有下一个ZipEntry,
// 输入流读取完成;
ZipEntry entry = in.getNextEntry();
while (entry != null) {
// 创建以zip包文件名为目录名的根目录
File file = new File(outputDirectory);
file.mkdir();
System.out.println("file.mkdir()");
if (entry.isDirectory()) {
String name = entry.getName();
name = name.substring(0, name.length() - 1);
file = new File(outputDirectory + File.separator + name);
file.mkdir();
} else {
file = new File(outputDirectory + File.separator + entry.getName());
file.createNewFile();
FileOutputStream resultFileOutput = new FileOutputStream(file);
byte[] buf = new byte[1024 * 1024];// 设定缓冲区大小
int bsize = 0;
while ((bsize = in.read(buf)) != -1) {
resultFileOutput.write(buf, 0, bsize);
}
//in.close();
resultFileOutput.close();
System.gc();
// FileOutputStream out = new FileOutputStream(file);
// int b;
// while ((b = in.read()) != -1) {
// out.write(b);
// }
// out.close();
}
// 读取下一个ZipEntry
entry = in.getNextEntry();
}
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}