Assert 目录文件拷贝时候, Android 有个规定就是文件大小不能操作1M, 不然会抛文件太大的错误. 解决办法如下. 将文件拷贝到类文件下:
代码示意如下:
private static boolean copyFile(Context ctx, String filename, String des) {
InputStream instream = null;
try {
if (filename.contains("TUIRes.ndt")) { //这个文件超过1M
instream = XmlFile.class.getResourceAsStream("TUIRes.ndt");
} else if (filename.contains("TUpdateRes.ndt")) {
instream = XmlFile.class.getResourceAsStream("TUpdateRes.ndt");
} else {
instream = ctx.getAssets().open(filename);
}
copyFile(des, instream);
return true;
} catch (Exception e) {
return false;
}
}
private static void copyFile(String fileToPath, InputStream in)
throws Exception {
OutputStream out = null;
try {
out = new FileOutputStream(fileToPath);
byte[] buffer = new byte[1024];
while (true) {
int ins = in.read(buffer);
if (ins == -1) {
break;
}
out.write(buffer, 0, ins);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.flush();
out.close();
}
}
}