title: Android lollipop write to sdcard from native code c++
body: <p>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.</p>
  
  <p><a href="https://stackoverflow.com/questions/26744842/how-to-use-the-new-sd-card-access-api-presented-for-lollipop">How to use the new SD card access API presented for Android 5.0 (Lollipop)?</a></p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>In c++ I am opening file with fdopen(fd). </p>
  
  <p>My questions are : </p>
  
  <ol>
  <li>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.</li>
  <li>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.</li>
  </ol>
  
  <p>Thank you</p>
  
  <p>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</p>