如何通过NDK访问Android Lollipop DocumentFile文件?

TSG*_*mes 13 android android-ndk android-5.0-lollipop

如何在原生JNI代码中访问Android KitKat/Lollipop中的DocumentFile生成的文件,因此我可以使用fopen,fread等.我特别要求它通过NDK访问外部SD卡文件.

Max*_*kiy 9

您可以使用文件描述符:

ParcelFileDescriptor filePfd;
DocumentFile file;
filePfd = getContentResolver().openFileDescriptor(file.getUri(), "w");
int fd = filePfd.getFd();
Run Code Online (Sandbox Code Playgroud)

这个int fd可以传递给JNI并用作通常的C++文件描述符:

FILE* file = NULL;
file = fdopen(fd, "r+b");
Run Code Online (Sandbox Code Playgroud)

您需要获得访问SD卡上的文件或目录的权限