android - 确定 Android Q 上现有的文件访问权限
问题描述
我正在从本机 C++ 代码读取和写入文件和目录。获取文件访问权限的标准方法是使用 Java 中的存储访问框架,然后在本机代码中使用文件描述符(https://developer.android.com/training/data-storage/files/media#native-code ):
// Called from native code:
public void requestFile() {
Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(intent, CREATE_REQUEST_CODE);
}
public native myNativeCallbackFunction(int fd, ...);
public void onActivityResult(int requestCode,
int resultCode, Intent resultData) {
Uri uri = resultData.getData();
ParcelFileDescriptor parcelFd = resolver.openFileDescriptor(uri, "r");
int fd = parcelFd.detachFd();
myNativeCallbackFunction(fd, "other information identifying the file such as the file name");
}
返回的 Uri fromonActivityResult
可以存储和重新获取,这样我们就不必每次都提示用户(https://developer.android.com/guide/topics/providers/document-provider#permissions):
final int takeFlags = intent.getFlags()
& (Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
// Check for the freshest data.
getContentResolver().takePersistableUriPermission(uri, takeFlags);
但是有些位置(app.package/files
例如)可以在没有 SAF 的情况下访问:
- 有没有办法确定是否可以在没有 SAF 的情况下访问文件/目录并且没有过去
onActivityResult
结果中存储的 Uri? - 有没有比使用函数方法更漂亮的将文件描述符传输到本机代码的
myNativeCallbackFunction
方法?
问候,
解决方案
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
删除该行。这没有道理。你不能在那里授予任何东西。
getFilesDir(), getExternalFilesDir() and getExternalFilesDirs()
是您仍然可以使用经典 File 类的地方。
进一步:原则上,您不必存储获得的 uri,因为getPermanentUriPermissions()
您可以随时看到它们。
但是,如果您拥有多个权限,您将不知道哪个权限是针对哪个权限的。所以也需要存储 self 。
推荐阅读
- objective-c - 为什么我的约束在我的 xib 文件中不起作用
- javascript - 如何将嵌套对象转换为一个对象
- python - 以非交互方式获取“帮助()”文本(Python)
- c - 为什么这段代码在第二行写输入?
- macos - 调试人偶错误:找不到依赖项
- ios - Xcode 错误“未能读取资产标签:意外错误生成结果标签”
- api - 在 RingCentral SCIM 2.0 API 中搜索/列出用户过滤器表达式语法
- python - 为什么我们需要在调用类函数时将 self 作为参数传递?
- r - 为字符添加双引号
- python - 从 Prodigy 的用于标记 NER 的 JSONL 格式转换为 spaCy 的训练格式?