How to save files in Android in downloads?
-
wrote on 28 Jul 2019, 15:35 last edited by Mikeeeeee
Hi!
How to save files in Android in downloads folder?
My code saves in Windows, but it seems for Android me need to somehow specify the path to the folder.
QFile *file = new QFile(downloadFolderAddress + nameFile);
if(file->open(QFile::WriteOnly)){
file->write(reply->readAll());
file->close(); -
Hi
Check out
https://doc.qt.io/qt-5/qstandardpaths.html
and the DownloadLocation -
wrote on 28 Jul 2019, 18:21 last edited by
So I found the download folders. But how to write the file? The file is not written by conventional means.
qDebug()<<"Downloads folder: "<<QStandardPaths::standardLocations(QStandardPaths::DownloadLocation);
Downloads folder: ("/storage/emulated/0/Download", "/storage/emulated/0/Android/data/org.qtproject.example.test/files/Download") -
Hi
its a list so it has 2 locations.
You should use one of them as path. -
wrote on 28 Jul 2019, 18:28 last edited by
Stops working before opening the file after
QFile *file = new QFile(downloadFolderAddress + nameFile); -
Stops working before opening the file after
QFile *file = new QFile(downloadFolderAddress + nameFile);@mikeeeeee
and how do you set downloadFolderAddress ? -
Hi,
To add to @mrjj, check that the folder you want to use exists and if not, create it. The locations retuned are valid but might not exist because there's not reason for them to have been created because your application might not use them at all.
And unless
nameFile
starts with a slash, your path is going to point to something invalid. -
Hi!
How to save files in Android in downloads folder?
My code saves in Windows, but it seems for Android me need to somehow specify the path to the folder.
QFile *file = new QFile(downloadFolderAddress + nameFile);
if(file->open(QFile::WriteOnly)){
file->write(reply->readAll());
file->close();@mikeeeeee
to add to the others, the Download location on Android is outside of the sandbox of your app, so you'll need to WRITE_EXTERNAL_STORAGE inside your manifest and eventually request/check permission before your write attempt -
wrote on 29 Jul 2019, 06:31 last edited by
I added _EXTERNAL_STORAGE. How to query/check the permission before trying to write to?
-
wrote on 29 Jul 2019, 06:35 last edited byThis post is deleted!
-
wrote on 29 Jul 2019, 07:10 last edited by
How to use? Do you have an example? In this code I get this error:
'QtAndroid::PermissionResult' is not contextually convertible to 'bool'
if (QtAndroid:: checkPermission("WRITE_EXTERNAL_STORAGE")) {qDebug()<<"good";} else {"bad";} -
How to use? Do you have an example? In this code I get this error:
'QtAndroid::PermissionResult' is not contextually convertible to 'bool'
if (QtAndroid:: checkPermission("WRITE_EXTERNAL_STORAGE")) {qDebug()<<"good";} else {"bad";}bool requestAndroidPermissions(){
//Request requiered permissions at runtimeconst QVector<QString> permissions({ "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"}); for(const QString &permission : permissions){ auto result = QtAndroid::checkPermission(permission); if(result == QtAndroid::PermissionResult::Denied){ auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission})); if(resultHash[permission] == QtAndroid::PermissionResult::Denied) return false; } } return true;
}
-
bool requestAndroidPermissions(){
//Request requiered permissions at runtimeconst QVector<QString> permissions({ "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"}); for(const QString &permission : permissions){ auto result = QtAndroid::checkPermission(permission); if(result == QtAndroid::PermissionResult::Denied){ auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission})); if(resultHash[permission] == QtAndroid::PermissionResult::Denied) return false; } } return true;
}
wrote on 12 May 2020, 10:16 last edited by@J-Hilk said in How to save files in Android in downloads?:
bool requestAndroidPermissions(){
//Request requiered permissions at runtimeconst QVector<QString> permissions({ "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE"}); for(const QString &permission : permissions){ auto result = QtAndroid::checkPermission(permission); if(result == QtAndroid::PermissionResult::Denied){ auto resultHash = QtAndroid::requestPermissionsSync(QStringList({permission})); if(resultHash[permission] == QtAndroid::PermissionResult::Denied) return false; } } return true;
}
You are wright !
I confirm you have to check permissions before writing file, EVEN if you have already asked permissions at the start of your app.