简体   繁体   English

Android棒棒糖从本机代码c ++写入sdcard

[英]Android lollipop write to sdcard from native code c++

I have android application which uses a lot of c++ native code. 我有android应用程序,它使用了很多c ++本机代码。 Application needs to work with files located on sdcard(read, create, append). 应用程序需要使用位于SD卡上的文件(读取,创建,追加)。 But Kitkat+ denied writing to sdcard for 3rd party applications. 但Kitkat +拒绝为第三方应用程序写入SD卡。 Android 5 introduced new API which allows that again. Android 5引入了新的API,允许再次使用它。

How to use the new SD card access API presented for Android 5.0 (Lollipop)? 如何使用为Android 5.0(Lollipop)提供的新SD卡访问API?

All examples and documentation what I found are mostly for Java side. 我发现的所有示例和文档主要用于Java方面。 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. 上面的链接包含有关如何获取可以返回ParcelFileDescriptor的DocumentFile的有价值的示例。 From this object I am able to receive native file descriptor - ParcelFileDescriptor.getFd(). 从这个对象我能够接收本机文件描述符 - ParcelFileDescriptor.getFd()。 It's integer which I am sending to c++ code through jni. 这是我通过jni发送到c ++代码的整数。

In c++ I am opening file with fdopen(fd). 在c ++中,我用fdopen(fd)打开文件。

My questions are : 我的问题是:

  1. Is fdopen function correct way how to open the file with new api? fdopen函数是否正确如何用新api打开文件? Or the DocumentFile already opens the file and I should only use fd in further operations. 或者DocumentFile已经打开文件,我应该只在进一步的操作中使用fd。
  2. It's enough to close the file descriptor inside native code with fclose? 使用fclose关闭本机代码中的文件描述符就足够了? or should I close it on java side with ParcelFileDescriptor.detachFd(). 或者我应该使用ParcelFileDescriptor.detachFd()在Java端关闭它。 Or both. 或两者。

Thank you 谢谢

EDIT : I getFD and detachFD works. 编辑:我getFD和detachFD工作。 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 但我从来没有找到答案如何正确替换ftruncate,它也需要写访问权限,而且我没有找到ftruncate版本,它接受文件描述符就像一个输入

1) yes, use file descriptors and fdopen 2) 1)是的,使用文件描述符和fdopen 2)

  1. Open the ParcelFileDescriptor . 打开ParcelFileDescriptor
  2. getFd(). getFd()。
  3. Pass the Fd to native code. 将Fd传递给本机代码。
  4. Close the ParcelFileDescriptor . 关闭ParcelFileDescriptor (this is closing your java reference) (这是关闭你的java引用)

Fd is just an int representing a linux id for a file. Fd只是一个表示文件的linux id的int In native: 原生:

  1. Fdopen Fdopen
  2. Do stuff 做东西
  3. Fclose (note this is closing your native file pointer) Fclose(注意这是关闭你的原生文件指针)

The two closes are doing different things. 两个关闭正在做不同的事情。

Note: You still need the SAF permission for the file or a higher root. 注意:您仍需要该文件或更高根的SAF权限。

Try Below Links: 尝试以下链接:

Android - writing/saving files from native code only: Android - writing/saving files from native code only Android - 仅从本机代码编写/保存文件: Android - 仅从本机代码编写/保存文件

Android NDK Write File: Android NDK Write File Android NDK写文件: Android NDK写文件

File Operations in Android NDK: File Operations in Android NDK Android NDK中的文件操作 :Android NDK中的文件操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM