How to access Android Lollipop DocumentFile files

2019-03-18 06:39发布

How is it possible to access files which are generated from a DocumentFile in Android KitKat/Lollipop in Native JNI Code, so I can use fopen, fread, etc. I'm particular asking this to access the external SD-Card files via the NDK.

1条回答
【Aperson】
2楼-- · 2019-03-18 07:35

You can use file descriptors:

ParcelFileDescriptor filePfd;
DocumentFile file;
filePfd = getContentResolver().openFileDescriptor(file.getUri(), "w");
int fd = filePfd.getFd();

This int fd can be passed to JNI and used as usual C++ file descriptor:

FILE* file = NULL;
file = fdopen(fd, "r+b");

And you need permission to access to file or directory on SD-card

查看更多
登录 后发表回答