4

I want to develop an App that uses the Storage Access Framework but processes the provided files with JNI native code.

Via the Storage Access Framework I get a Uri after a call to startActivityForResult() with a Intent.ACTION_OPEN_DOCUMENT intent. From this I can get an InputStream, ParcelFileDescriptor or similar.

Is there a way to get from this something (like for example a FILE) that I can pass on to my native code so that it can directly read from/write to the file provided by the content provider?

Background:

The C part of the JNI interface (which I didn't write but can modify/extend) can load two kinds of data

  1. files specified by a file name const char *filename that can be opened with open()
  2. data in a buffer jbyteArray array containing the bytes that make up the "file"

and can write to

  1. a file path const char *filename that can be opened with fopen()

Currently, when opening a file with Intent.ACTION_OPEN_DOCUMENT the Uri uri obtained in onActivityResult() is converted to an input stream with getContentResolver().openInputStream(uri) and then read into a byte[] buffer which is passed to the native code. Once the native code is done, it saves the result to a temporary file whose path is passed back to the Java side. The Java code then reads the temporary file and puts its content into a FileOutputStream obtained from the uri.

This is obviously horribly inefficient and ugly.

cgogolin
  • 960
  • 1
  • 10
  • 22
  • Hi, have you solved this? I have similar problem, I need to access files on external sdcard by absolute path from native code. I thought I can grant access to the sdcard by SAF and then it will be accessible and writable, but it seems it's not possible... Thanks. – barbucha Jan 11 '16 at 12:13
  • No. Unfortunately there doesn't seem to be a slick solution for this. – cgogolin Jan 11 '16 at 12:36
  • 3
    maybe this will help you : [here](https://stackoverflow.com/questions/30593964/how-to-access-android-lollipop-documentfile-files-via-ndk/31677287) – 马liow Jun 15 '18 at 12:03

0 Answers0