1、创建 fd ,jni 接口
public static native int openFileFromNative(FileDescriptor fileDescriptor);
2、java 文件获取文件指针
ParcelFileDescriptor pfd = = getContentResolver().openFileDescriptor(filePathUri, "rw");
FileDescriptor fd = pfd.getFileDescriptor();
openFileFromNative(fd);
3、C++ 调用函数
static int jniGetFDFromFileDescriptor(JNIEnv * env, jobject fileDescriptor) {
jint fd = -1;
jclass fdClass = env->FindClass("java/io/FileDescriptor");
if (fdClass != NULL) {
jfieldID fdClassDescriptorFieldID = env->GetFieldID(fdClass, "descriptor", "I");
if (fdClassDescriptorFieldID != NULL && fileDescriptor != NULL) {
fd = env->GetIntField(fileDescriptor, fdClassDescriptorFieldID);
}
}
LOGD("jni Get FD From File Descriptor %d",fd);
return fd;
}
// jni对应函数
static jint openFileFromNative(JNIEnv* env, jobject clazz, jobject fileDescriptor) {
int fd = jniGetFDFromFileDescriptor(env, fileDescriptor);
//todo
}