Android and iOS File Read and Write using QML
Unsolved
Mobile and Embedded
-
Hi,
Purely on QML ? AFAIK, no. But you can provide your own C++ extension that will do it and use it from your QML code.
-
this works on android and iOS:
bool ApplicationUI::checkDirs() { // Android: HomeLocation works, iOS: not writable // Android: AppDataLocation works out of the box, iOS you must create the DIR first !! mDataPath = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0); qDebug() << "Data Path: " << mDataPath; QDir myDir(mDataPath); if (!myDir.exists()) { bool ok = myDir.mkpath(mDataPath); if(!ok) { qWarning() << "Couldn't create dir. " << mDataPath; return false; } qDebug() << "created directory path" << mDataPath; } return true; }
now you can read and write as usual
QFile readFile(mDataPath+"/yourfile.txt"); if(!readFile.exists()) { qDebug() << "file doesn't exist "; return false; } if (!readFile.open(QIODevice::ReadOnly)) { qWarning() << "Couldn't open file"; return false; }
took me some time to figure out that AppDataLocation already exists on Android, but must be created on iOS
more infos:
https://developer.android.com/guide/topics/data/data-storage.html#filesInternal
https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html
http://doc.qt.io/qt-5/qstandardpaths.html#standardLocations -
Thanks @ekkescorner , your solution worked superbly. Much appreciated.