Reputation: 917
I have android application which uses a lot of c++ native code. Application needs to work with files located on sdcard(read, create, append). But Kitkat+ denied writing to sdcard for 3rd party applications. Android 5 introduced new API which allows that again.
How to use the new SD card access API presented for Android 5.0 (Lollipop)?
All examples and documentation what I found are mostly for Java side. Examples for native code don't exist or are very unclear. So I want ask few questions.
The link above contains valuable example how to get DocumentFile which can return ParcelFileDescriptor. From this object I am able to receive native file descriptor - ParcelFileDescriptor.getFd(). It's integer which I am sending to c++ code through jni.
In c++ I am opening file with fdopen(fd).
My questions are :
Thank you
EDIT : I getFD and detachFD works. But I never found answer how to correctly replace ftruncate, which needs write access too, and I did not found ftruncate version which takes file descriptor like a input
Upvotes: 3
Views: 3536
Reputation: 7828
1) yes, use file descriptors and fdopen 2)
ParcelFileDescriptor
.ParcelFileDescriptor
. (this is closing your java reference)Fd is just an int
representing a linux id for a file. In native:
The two closes are doing different things.
Note: You still need the SAF permission for the file or a higher root.
Upvotes: 0
Reputation: 49
Try Below Links:
Android - writing/saving files from native code only: Android - writing/saving files from native code only
Android NDK Write File: Android NDK Write File
File Operations in Android NDK: File Operations in Android NDK
Upvotes: -1