1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
size_t copy(JNIEnv *env, jobject assetManager, jstring srcName, jstring dstName) {
std::string src_name =jstringToString(env,srcName);
std::string dst_name =jstringToString(env,dstName);
AAssetManager *mgr = AAssetManager_fromJava(env, assetManager);
AAsset *asset = AAssetManager_open(mgr, src_name.c_str(), AASSET_MODE_UNKNOWN);
ofstream dst(dst_name, ios::binary);
//文件大小
size_t totalSize = AAsset_getLength(asset);
//这里分批次读取,以防大文件内存崩溃
const size_t bufSize = 1024;
char *buffer = (char *) malloc(sizeof(char) * bufSize);
int readSize = 0;
while ((readSize = AAsset_read(asset, buffer, bufSize))) {
dst.write(buffer, readSize);
}
dst.flush();
dst.close();
AAsset_close(asset);
free(buffer);
return totalSize;
}
|