How to save files in Android in downloads?
-
Hi
Check out
https://doc.qt.io/qt-5/qstandardpaths.html
and the DownloadLocation -
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. -
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 -
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;
}
@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.