Can't save file to external storage on android
-
I'm trying to write to an external SdCard with QFile, running Qt 5.10 and android 5.1. And I get a permission denied error. It is working with the internal storage.
#include <QCoreApplication> #include <QFile> #include <QTextStream> #include <QDebug> #ifdef __ANDROID__ #include <QtAndroid> #endif bool check_permission() { QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" ); r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { qDebug() << "Permission denied"; return false; } } qDebug() << "Permissions granted!"; return true; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if (!check_permission()) return -1; QFile f("/storage/extSdCard/WriteTest.txt"); if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Error while opening file to save"; qDebug() << f.errorString(); return -1; } QTextStream out(&f); out << "This is just a test\n"; return a.exec(); }
whhere check_permission is a function I took from Qt's bugtracker that needs to be used with Qt 5.10 > and android SDK > 22. The thing is I'm at android SDK < 22.
here is the function just in case. I stepped into it and it returns true on checkPermission (the external storage write flag is set in the androidManifest too)
And my output
Permissions granted! Error while opening file to save "Permission denied"
-
I'm trying to write to an external SdCard with QFile, running Qt 5.10 and android 5.1. And I get a permission denied error. It is working with the internal storage.
#include <QCoreApplication> #include <QFile> #include <QTextStream> #include <QDebug> #ifdef __ANDROID__ #include <QtAndroid> #endif bool check_permission() { QtAndroid::PermissionResult r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { QtAndroid::requestPermissionsSync( QStringList() << "android.permission.WRITE_EXTERNAL_STORAGE" ); r = QtAndroid::checkPermission("android.permission.WRITE_EXTERNAL_STORAGE"); if(r == QtAndroid::PermissionResult::Denied) { qDebug() << "Permission denied"; return false; } } qDebug() << "Permissions granted!"; return true; } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); if (!check_permission()) return -1; QFile f("/storage/extSdCard/WriteTest.txt"); if (!f.open(QIODevice::WriteOnly | QIODevice::Text)) { qDebug() << "Error while opening file to save"; qDebug() << f.errorString(); return -1; } QTextStream out(&f); out << "This is just a test\n"; return a.exec(); }
whhere check_permission is a function I took from Qt's bugtracker that needs to be used with Qt 5.10 > and android SDK > 22. The thing is I'm at android SDK < 22.
here is the function just in case. I stepped into it and it returns true on checkPermission (the external storage write flag is set in the androidManifest too)
And my output
Permissions granted! Error while opening file to save "Permission denied"
I think you may have to give also permissions on the Android device itself for the application. At least what I found when adding an external SD card after the application had been installed already.
I followed this video and had to use my imagination because my Android device is in German and it is also Android 7. However after granting rights in settings, I was ableto copy to external SD card.
Hope it helps in your case.
-
I have the same problem (Qt5.12.0). Did you find a solution?
I can write files to "/emulated/0/" but not to "/storage".
Permissions are set, I integrated the permission-dialog and hit accept. App-Settings permission is set (WRITE_EXTERNAL_STORAGE).
(READ_EXTERNAL_STORAGE is only set in Manifest.)I tried writing with QFile and in Qml with XMLHttpRequest's "PUT" method.
I tried Urls with "file:///", "file://", "/" and "".I get "QIODevice::write (QFile, "/storage/3962-3732/[...]"): device not open.
-
I have the same problem (Qt5.12.0). Did you find a solution?
I can write files to "/emulated/0/" but not to "/storage".
Permissions are set, I integrated the permission-dialog and hit accept. App-Settings permission is set (WRITE_EXTERNAL_STORAGE).
(READ_EXTERNAL_STORAGE is only set in Manifest.)I tried writing with QFile and in Qml with XMLHttpRequest's "PUT" method.
I tried Urls with "file:///", "file://", "/" and "".I get "QIODevice::write (QFile, "/storage/3962-3732/[...]"): device not open.
@Dabulla hi,
is your android version >= 6 ?
Because in the android documentation it says:In Android 6.0, any device that is not adopted is considered portable. Because portable storage is connected for only a short time, the platform avoids heavy operations such as media scanning. Third-party apps must go through the Storage Access Framework to interact with files on portable storage; direct access is explicitly blocked for privacy and security reasons.
I don't think Qt's QFile goes natively through the
Storage Access Framework
. However, I may be wrong if someone knows more about it... -
@Dabulla I solved same problem with this library:
https://github.com/FalsinSoft/QtAndroidToolsAlso, you should read this document about permission problem:
https://falsinsoft.github.io/QtAndroidTools/Documentation/#AppPermissionsI hope it helps someone.
-
Hello,
As I read you have an issue that you cant save the file in internal storage.
This issue coming due to you have given the internal storage path.
Please check you have given a right path for application to save files.
If you have done this process then you have to give all the permission to your Qt application
Then your file will be saved where you want.
Hope this will help you
Thanks -
you should use android uri for the file in order to meet android storage access framework conditions.
when working with android uri you can use CrossQFile to work with the file.
https://github.com/mahdize/CrossQFilehttps://developer.android.com/guide/topics/providers/document-provider
-
Is sharing data what you actually want to achieve? Look here: https://github.com/ekke/ekkesSHAREexample