Android and iOS File Read and Write using QML
-
wrote on 29 Jun 2016, 12:52 last edited by Bongani
I am developing a native mobile App for both android and iOS device, is it possible to create text files in the phone's internal storage and read from them using QML ?
-
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 -
wrote on 26 Jul 2016, 10:01 last edited by
Thanks @ekkescorner , your solution worked superbly. Much appreciated.