Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Android Lollipop DocumentFile files via NDK?

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.

like image 783
TSGames Avatar asked Jun 02 '15 10:06

TSGames


People also ask

What is documentfile in Android?

androidx.documentfile.provider.DocumentFile. Representation of a document backed by either a DocumentsProvider or a raw file on disk. This is a utility class designed to emulate the traditional File interface. It offers a simplified view of a tree of documents, but it has substantial overhead.

Can I use other developers’ libraries in my Android NDK?

It is also possible to use other developers’ libraries or your own if there is something that you absolutely need to use. The “ndk-build” script is the heart of the Android NDK and it is responsible for automatically going through your project and determining what to build.

How to secure Android keys using ndk-build?

***Securing Keys using ndk-build… (Item-01) step 01: Create a folder “jni” under src/main. step 02: Create a file ‘Android.mk’ under “jni” folder with following content: step 03: Create another file ‘Application.mk’ file under “jni” folder with the following content:

What is the Android native development kit (NDK)?

The Android Native Development Kit (NDK): a set of tools that allows you to use C and C++ code with Android. CMake: an external build tool that works alongside Gradle to build your native library.


1 Answers

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

like image 145
Maxim Metelskiy Avatar answered Sep 20 '22 17:09

Maxim Metelskiy