Android lollipop write to sdcard from native code c++ - android

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 :
Is fdopen function correct way how to open the file with new api? Or the DocumentFile already opens the file and I should only use fd in further operations.
It's enough to close the file descriptor inside native code with fclose? or should I close it on java side with ParcelFileDescriptor.detachFd(). Or both.
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

1) yes, use file descriptors and fdopen
2)
Open the ParcelFileDescriptor.
getFd().
Pass the Fd to native code.
Close the ParcelFileDescriptor. (this is closing your java reference)
Fd is just an int representing a linux id for a file. In native:
Fdopen
Do stuff
Fclose (note this is closing your native file pointer)
The two closes are doing different things.
Note: You still need the SAF permission for the file or a higher root.

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

Related

Accessing Android USB Mass Storage Using Delphi

I am trying to write an Android App in Delphi 10.4 (primarily for AndroidTV) using Delphi which can read or write files from/to USB mass storage. I can see the files using FileManager App, but have failed to see them with my App. I found a library on GitHub (Libaums) which would appear to do all I want and much more, but is written in a mixture of Kotlin, Java, and C, which would appear to be very difficult to integrate with Delphi.
Edit: The paths I have tried include: GetDataDirectory(), GetRootDirectory(), GetExternalStorageDirectory(),GetSysExternalStorageDirectory(), GetExternalStoragePublicDirectory(), /mnt/usb001, /mnt/usb002, /mnt/androidwritable, /mnt/user.
The device in mind currently uses SDK 28, but I would like a solution that would still work on later versions.
All my attempts to get file paths report non-existent, which I take to mean I have a permissions problem. Can anyone:
(1) Advise which file path to request?
(2) How to obtain the relevant permissions?
TIA

How to give NDK/Native code same file permissions as Java?

My app is letting users select audio files with an Intent.ACTION_OPEN_DOCUMENT from within their document hierarchy (ordinarily on internal storage and not in my app's storage). The files, once selected, are then processed natively in OBOE.
I have JNI working. I can open the file in java and copy the bytecode over to native.
But if I want to open a file directly in the Native code then I must set the app to ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION. This seems a bit clumsy, it is probably because I'm sending the file path - using getPath() on the uri - to native and using fopen()?
Is it possible for me to inherit the permission from the java side to open the file natively without requiring All Files Acess permission? I tried sending a FileDescriptor over but that wasn't successful, although could have been my implementation.

Opening Files from a String Path with Android NDK

I've been going over the Android file access documentation lately, but I seem to be unable to figure out how to actually open a file given as a string containing the path to the file I'd like to open.
What I (eventually) want to accomplish is something like this:
The user selects a specific kind of text file using Intents, receiving a URI to the file. From this I derive the path (getPath()) and pass this string to the native C++ code.
The native C++ opens the file from the string, parsing the content.
Perform some actual work with the above.
From what I've found so far, it seems like it is no longer possible to open files this way (as of SDK version 26 at least):
A hard-coded path to a file I know exists gives me permission denied.
The path itself received from getPath() triggers a No such file or directory error.
One workaround called for opening the file on the Java side using the ContentResolver, and then passing the file descriptor to the native side. This works, but it's problematic: the files can contain references to other files to be opened ("include files") making such a solution of limited use.
Just to make things clear, these files reside locally on the "USB" partition of the Android system, unrelated to the app itself. Not as resources/assets to the APK or anything similar which other questions of this kind seem to require.
In summary, I guess the question is this: Is it possible to open a file, and possibly any other files it refers to, given a path from the Java side of the application? Is there any requirements for doing this, such as requesting the correct permissions for folders or something similar?
As of Android 6.0 (API level 23) you need to request permissions every time your app starts for "dangerous actions" such as accessing the filesystem or reading contacts. The linked pages already have a snippet of code you can copy.
On older phones requesting permissions in the manifest was sufficient, but the target SDK version was recenly upped to 8.0 (=26). If you want to support devices pre-6.0, the Android compatibility library will allow you to call the same API.

Recognize native android file

I want to write a file classificator - something like the linux file command does. The main feature should be to detect the target OS (especially Android). It has to work on compiled/native files (ELF file format). For executables I currently check the interpreter (/system/bin/linker) to guess it is android. For shared objects I do not have such a criteria yet. I there a way to do that?

Creating a .pcap file in native code in Android then retrieving it

I have an app that uses native code to generate a .pcap file (amongst other things). The idea is that while running the program, the pcap file is created and I can later retrieve it by say plugging in the phone into my laptop and then access it.
I use fopen(), fwrite() and fclose() in my native code to create the .pcap file. My question is, how should I specify the filename and filepath? For example, when I run it on windows, I just use "test.pcap" and it generates it in the same folder, but how does this work on android? As I mentioned before, the file is created down in native level.
Native or not, the directory structure should be the same.
You can create a directory in /flash called pcap. But check it exists first, as it can be /nand. Use /sdcard if you want to write in the SD card.
After that you can use, say, /flash/pcap/test.pcap as path for the file. The file would be created in the internal flash memory.

Categories

Resources