[英]Write byte[] passed through jbytearray to a file
我需要將使用jni傳遞給C ++函數的字節數組寫入文件。
那是我的Java代碼
private void writeData(byte[] array) {
Native nativeobject = new Native();
long tsize = this.size;
long incremental = 0;
while(tsize != 0 && !this.stopped) {
nativeobject.writeFile(whereToSave, array);
publishProgress(incremental);
incremental += TRANCE_SIZE;
tsize -= TRANCE_SIZE;
}
}
那就是我的函數頭
JNIEXPORT jbyteArray JNICALL Java_it_mls_secureeraser_algorithms_Native_writeFile(JNIEnv *jni, jobject thiz,
jstring jfileName, jbyteArray jarray) {
const char *fileName = jni->GetStringUTFChars(jfileName, 0);
int len = jni->GetArrayLength (jarray);
unsigned char* buf = new unsigned char[len];
jni->GetByteArrayRegion (jarray, 0, len, reinterpret_cast<jbyte*>(buf));
FILE *output = fopen(fileName, "a+");
fwrite(buf, sizeof(unsigned char*), sizeof(buf), output);
fclose (output);
}
如何從jbytearray傳遞到可以寫入文件的內容? 感謝幫助
在這里看看: 將Java中的byte []轉換為C ++中的unsigned char *的正確方法,反之亦然? 。 我之前也遇到過類似的問題,這就是解決方案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.