Search code examples
c++qtqmlqtcoreqtmultimedia

QML data folder


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.


Solution

  • /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.