QFile on Android!
-
Hello.. I'm trying to open a flat TXT file on Android.
This way, with the file inside the program folder, I can read it:
QFile file( "/data/user/0/com.digitalsof.pontofacilapp38/files/TEST.TXT" ); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
But this way, with the file in the Documents folder I can't read:
QFile file( "/storage/emulated/0/Documents/TEST.TXT" ); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
Apparently it's an Android permissions issue.
But how to grant permission to read in Documents folder??
And the curious thing that I can save in the Documents folder. I just can't read.
I use QT 5.14.2
-
Hello.. I'm trying to open a flat TXT file on Android.
This way, with the file inside the program folder, I can read it:
QFile file( "/data/user/0/com.digitalsof.pontofacilapp38/files/TEST.TXT" ); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
But this way, with the file in the Documents folder I can't read:
QFile file( "/storage/emulated/0/Documents/TEST.TXT" ); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) return;
Apparently it's an Android permissions issue.
But how to grant permission to read in Documents folder??
And the curious thing that I can save in the Documents folder. I just can't read.
I use QT 5.14.2
Check the answers here:
https://forum.qt.io/topic/105406/how-to-save-files-in-android-in-downloads
should not have changed since then
-
I perform the permissions check when opening the APP and before trying to open the file:
using namespace QtAndroid; //SOPermission sop; QStringList permissions = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.INTERNET"}; const QHash<QString, PermissionResult> results = requestPermissionsSync(permissions); auto ok = true; auto i = 0; while (ok && i< permissions.size()) { if (!results.contains(permissions[i]) || results[permissions[i]] == PermissionResult::Denied) { qCritical() << "Couldn't get permission: " << permissions[i]; ok = false; --i; } ++i; } return ok;
But without success. The problem continues. Any idea?
-
I perform the permissions check when opening the APP and before trying to open the file:
using namespace QtAndroid; //SOPermission sop; QStringList permissions = {"android.permission.READ_EXTERNAL_STORAGE", "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.INTERNET"}; const QHash<QString, PermissionResult> results = requestPermissionsSync(permissions); auto ok = true; auto i = 0; while (ok && i< permissions.size()) { if (!results.contains(permissions[i]) || results[permissions[i]] == PermissionResult::Denied) { qCritical() << "Couldn't get permission: " << permissions[i]; ok = false; --i; } ++i; } return ok;
But without success. The problem continues. Any idea?
@RenanHm probably you're dealing with SDK 30+?
you can try FileDialog to see if you get access there.
FileDialog should open the native Android FileDialogFileDialog { id: importFileDialog title: "Select a file to import" selectExisting: true selectFolder: false selectMultiple : false folder: shortcuts.documents onAccepted: console.log(myFileHelper.readFile(fileUrl)) }
cpp:
Q_INVOKABLE QString readFile(const QUrl &contentURL) const { QFile file(contentURL.toString()); if(!file.open(QFile::ReadOnly | QFile::Text)) { return "Read failed"; } QTextStream in(&file); QString myText = in.readAll(); file.close(); return myText; }
if you don't want to use FileDialog take a look at KDAB's SharedStorage: https://www.kdab.com/android-shared-storage-qt-wrapper/ and try out the example app if this library could be helpful.
also I found out that QDir and QFile on Android not only accept filePathes, but also content URIs. (not documented: https://bugreports.qt.io/browse/QTBUG-99664)
if all of this doesn't help - probably we have to wait until Qt has finalized support of Android (https://bugreports.qt.io/browse/QTBUG-98974) ScopedStorage.