Qt for Android存储更新

Qt for Android Storage Updates

Qt for Android存储更新

March 07, 2023 by Assam Boudjelthia | Comments

2023年3月7日,Assam Boudjelthia |评论

Keeping up with the constant changes in Android's APIs, Qt is adding some improvements when managing files and content URIs under the restrictions introduced with Android's Scoped Storage.

为了跟上Android API的不断变化,Qt在管理文件和内容URI时添加了一些改进,以满足Android的范围存储所引入的限制。

Improvements for content URIs handling

内容URI处理的改进

Now, Qt classes support a few more operations that have been missing when dealing with content scheme URIs. Qt's Android content file engine can now handle creating, removing, renaming, and moving files and directories. However, this is available only when possible and when the appropriate permissions are available for example through the Android Storage Framework that's accessible through QFileDialog. We implement a hierarchy similar to Android's DocumentFile and use DocumentsContract for some file operations under the hood.

现在,Qt类支持处理内容方案URI时缺少的其他一些操作。Qt的Android内容文件引擎现在可以处理创建、删除、重命名和移动文件和目录。然而,这仅在可能的情况下可用,并且在适当的权限可用时可用,例如通过QFileDialog访问的Android存储框架。我们实现了一个类似于Android的DocumentFile的层次结构,并在后台使用DocumentsContract进行一些文件操作。

Furthermore, since content URIs have their own format where only part of the path is percent-encoded, it was hard to deal with file names. Now, Qt's classes like QFile, QDir and QFileInfo got fixed to properly deal with content URIs. These include properly returning absolute paths, file and base names, dir names, etc.

此外,由于内容URI有自己的格式,其中只有部分路径是百分比编码的,所以很难处理文件名。现在,Qt的类(如QFile、QDir和QFileInfo)得到了修复,以正确处理内容URI。其中包括正确返回绝对路径、文件和基名称、目录名称等。

As part of these changes, a bug where using QDirIterator was returning unusable subdirectories paths or fully stuck on an infinite loop when iterating certain directories, is now fixed as well.

作为这些更改的一部分,使用QDirIterator返回不可用的子目录路径或在迭代某些目录时完全陷入无限循环的错误现在也得到了修复。

With those changes, it's worth mentioning, we don't yet fully integrate with the MediaStore API and SAF via QFileDialog can still be used to grant access when needed.

值得一提的是,通过这些更改,我们尚未与MediaStore API和SAF完全集成。在需要时,仍可使用QFileDialog授予访问权限。

Android 10, brings about Scoped Storage with various access restrictions, see app access restrictions. However, Android 11 adds a new permission MANAGE_EXTERNAL_STORAGE which allows full storage access that can go around the restrictions ( while requiring a manual review by Play Store).

Android 10带来了具有各种访问限制的范围存储,请参阅应用程序访问限制。然而,Android 11增加了一个新的权限MANAGE_EXTERNAL_STORAGE,它允许完全访问存储,可以绕过限制(同时需要Play商店手动审核)。

QStandardPaths

默认路径

With the introduction of Scoped Storage, it's recommended to use app-specific directories over external public directories. The latter directories will still be returned by QStandardPaths::writableLocation() when usable, otherwise, the app private locations are returned based on the runtime Android version. Along, the way, a bug where potentially duplicate paths were returned by QStandardPaths::standardLocations() was fixed. Appropriate notes were added to our documentation.

随着范围存储的引入,建议使用特定于应用程序的目录而不是外部公共目录。当可用时,QStandardPaths::writableLocation()仍将返回后一个目录,否则,将基于运行时Android版本返回应用程序的私有位置。顺便说一句,QStandardPaths::standardLocations()返回了可能重复路径的错误得到了修复。我们的文档中添加了适当的注释。

Note that, all the changes mentioned so far are back ported to the currently supported Qt releases.

注意,到目前为止提到的所有更改都被移植到当前支持的Qt版本。

QDesktopServices and FileProvider

QdeskTopservices和FileProvider

Qt's QDesktopServices now support Android's FileProvider under the hood. This allows Qt apps to easily open or share files that can be stored under the app's private storage, with other apps. Thus, Qt's default manifest will come with an initial file provider element. The quick snippet below creates a file under the app's private/sandbox storage and when calling openUrl(), Qt uses FileProvider facilities to obtain a shareable content URI.

Qt的QDesktopServices现在支持Android的FileProvider。这使得Qt应用程序可以轻松地打开或与其他应用程序共享可以存储在应用程序专用存储下的文件。因此,Qt的默认清单将附带一个初始文件提供程序元素。下面的快速片段在应用程序的私有/沙盒存储下创建了一个文件,当调用openUrl()时,Qt使用FileProvider工具获取可共享的内容URI。

const auto standardPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const auto fileName = standardPath + "/text.txt";
QFile file(fileName);
file.open(QIODevice::WriteOnly);
file.write("Sample text");
file.close();

QDesktopServices::openUrl(fileName);

Note that, this effectively adds AndroidX dependency by default to Gradle configuration file (i.e. build.gradle).

注意,默认情况下,这有效地将AndroidX依赖项添加到Gradle配置文件(即build.Gradle)中。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值