QML 数据文件夹

2022-01-19 00:00:00 qt qml qtmultimedia c++ qtcore

我是基于 QML 的应用程序,我使用 CameraCameraCapture 从相机捕获图像.捕获后,我想使用 CameraCapture.captureToLocation() 将捕获的图像存储在应用程序数据文件夹中.但我不知道如何获取该文件夹的路径.所以我的问题-如何获得具有写入权限的应用程序文件夹的路径?Qt有没有办法得到它?我猜它应该是系统指定的文件夹.例如在 Android 中它应该是/data/data/AppName.正如我所见,LocalStorage 在类似的地方创建它的文件.

解决方案

/data/data/AppName 没有映射到 QStandardPaths,但我认为使用这个会更好:

<块引用>

QStandardPaths::AppDataLocation 17 返回可以存储持久应用程序数据的目录位置.这是一个特定于应用程序的目录.要获取存储要与其他应用程序共享的数据的路径,请使用 QStandardPaths::GenericDataLocation.返回的路径永远不会为空.在 Windows 操作系统上,这将返回漫游路径.这个枚举值是在 Qt 5.4 中添加的.

或早期版本中的这个:

<块引用>

QStandardPaths::DataLocation 9 返回与 AppLocalDataLocation 相同的值.此枚举值已弃用.最好使用 AppDataLocation,因为在 Windows 上,建议使用漫游路径.

这些将在 Android 上设置为 DataLocation /files"、//files".

但请注意,从 5.4 开始,后者已被弃用,如文档所述.然后您可以将其设置为 follows:p><块引用>

QString QStandardPaths:: writableLocation(StandardLocation 类型)

返回类型文件应该写入的目录,如果位置无法确定,则返回空字符串.

注意:返回的存储位置可以是不存在的目录;即,它可能需要由系统或用户创建.

所以,你会这样写:

QString dataPath = QStandardPaths:: writableLocation(QStandardPaths::AppDataLocation);//不需要根据文档检查此值的空字符串CameraCapture.captureToLocation(dataPath);

注意:请不要将 QStandardPaths::ApplicationsLocation 用于此目的,因为那是非专用位置.此外,它甚至在 Android 上也不支持:

<块引用>

ApplicationsLocation 不受支持(目录不可读)

如果您想在应用程序之间共享图像,您需要对上面的代码稍作调整来使用它.虽然我认为你想要上面的那个,但有点不清楚你想要哪一个,所以我提到了两个:

<块引用>

StandardPaths::GenericDataLocation 11 返回可以存储跨应用程序共享的持久数据的目录位置.这是一个通用值.返回的路径永远不会为空.

这在 Android 上设置为 GenericDataLocation "<USER>".类文档中提到了确切的映射,您只需向下滚动到表格.我无法提供直接 URL,因为该部分没有永久链接.你也可以自己检查一下,哪个最合适.

Im my app based on QML I use Camera and CameraCapture to capture an image from the camera. After it was captured I want to store captured image in application data folder with CameraCapture.captureToLocation(). But I have no idea how to get path to this folder. So my question - how can I get path to application folder wit write permissions? Is there way in Qt to get it? It should be system specified folder, I guess. For example in Android it should be /data/data/AppName. As I see LocalStorage creates its files in some similar place.

解决方案

/data/data/AppName is not mapped in QStandardPaths, but I think it would be better to use this one:

QStandardPaths::AppDataLocation 17 Returns a directory location where persistent application data can be stored. This is an application-specific directory. To obtain a path to store data to be shared with other applications, use QStandardPaths::GenericDataLocation. The returned path is never empty. On the Windows operating system, this returns the roaming path. This enum value was added in Qt 5.4.

or this one in earlier versions:

QStandardPaths::DataLocation 9 Returns the same value as AppLocalDataLocation. This enumeration value is deprecated. Using AppDataLocation is preferable since on Windows, the roaming path is recommended.

These will be set to DataLocation "<APPROOT>/files", "<USER>/<APPNAME>/files" underneath on Android.

But note that from 5.4 on, the latter is deprecated as documented. You can set it then as follows:

QString QStandardPaths::?writableLocation(StandardLocation type)

Returns the directory where files of type should be written to, or an empty string if the location cannot be determined.

Note: The storage location returned can be a directory that does not exist; i.e., it may need to be created by the system or the user.

So, you would write something like this:

QString dataPath = QStandardPaths::?writableLocation(QStandardPaths::AppDataLocation);
// Do not need to check against empty string for this value as per documentatio
CameraCapture.captureToLocation(dataPath);

Note: Please do not use QStandardPaths::ApplicationsLocation for this purpose since that is the non-dedicated location. In addition, it is even unsupported on Android:

ApplicationsLocation not supported (directory not readable)

If you want to share the image across applications, you would need to use this with minor adjustment to the code above. While I think you wanted the one above, it is a bit unclear which one you wanted, so I am mentioning both:

StandardPaths::GenericDataLocation 11 Returns a directory location where persistent data shared across applications can be stored. This is a generic value. The returned path is never empty.

This is set to GenericDataLocation "<USER>" on Android. The exact mappings are mentioned in the class documentation, you just need to scroll down to the tables. I cannot give direct URL as there is no permalink to that section. You can check yourself, too, which fits the best.

相关文章