简体   繁体   English

使用Android NDK从字符串路径打开文件

[英]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. 我最近浏览了Android文件访问文档,但是似乎无法弄清楚如何实际打开一个文件,该文件以包含要打开的文件路径的字符串形式给出。

What I (eventually) want to accomplish is something like this: 我(最终)想要完成的工作是这样的:

  1. The user selects a specific kind of text file using Intents, receiving a URI to the file. 用户使用Intents选择特定类型的文本文件,并接收该文件的URI。 From this I derive the path ( getPath() ) and pass this string to the native C++ code. 由此,我导出路径( getPath() )并将此字符串传递给本机C ++代码。
  2. The native C++ opens the file from the string, parsing the content. 本机C ++从字符串中打开文件,解析内容。
  3. 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): 根据到目前为止的发现,似乎不再可能以这种方式打开文件(至少从SDK版本26开始):

  • A hard-coded path to a file I know exists gives me permission denied . 我知道存在的文件的硬编码路径会授予我permission denied
  • The path itself received from getPath() triggers a No such file or directory error. getPath()接收到的路径本身会触发No such file or directory错误。

One workaround called for opening the file on the Java side using the ContentResolver , and then passing the file descriptor to the native side. 一种解决方法是使用ContentResolver在Java端打开文件,然后将文件描述符传递到本机端。 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. 为了清楚起见,这些文件位于Android系统的“ USB”分区本地,与应用程序本身无关。 Not as resources/assets to the APK or anything similar which other questions of this kind seem to require. 不能作为APK的资源/资产或其他类似问题所需的资源。

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? 总而言之,我想问题是这样的:给定应用程序Java端的路径,是否可以打开文件以及它所引用的其他文件? 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. 从Android 6.0(API级别23)开始,每次应用启动时,您都需要请求危险操作 ”(例如访问文件系统或读取联系人)的权限 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). 在较旧的手机上,在清单中请求许可就足够了,但是目标SDK版本最近被提高到8.0(= 26)。 If you want to support devices pre-6.0, the Android compatibility library will allow you to call the same API. 如果要支持6.0之前的设备,则Android兼容性库将允许您调用相同的API。

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

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