[Interest] Save / load file contents on android

Nuno Santos nuno.santos at imaginando.pt
Fri Sep 3 11:05:02 CEST 2021


Ekke,

I have been experiencing some inconsistencies with Android file access using QStandardPaths, mostly now that I’m preparing the ground for Android 11 support. From Android 11 onwards, there will be changes in the way an app can access filesystem resources.

This problem started when one of apps was in some systems using legacy file access while in others was already using the new storage approach. In order to temporary fixes this issues I have set the requestLegacyExternalStorage to true, however, soon this will end as in Android 11 it will be ignored. 

One of the current challenges I’m facing the moving/copying resources from legacy storage to new storage when using QStandardPaths and QFileDialog.

How have you been handling Android 11 support and data migration from older versions?

Are you able to do it just with Qt stuff or do you need to handle some of the things with native android API’s?

Thanks!

Nuno

> On 31 Aug 2021, at 13:38, ekke <ekke at ekkes-corner.org> wrote:
> 
> Am 31.08.21 um 14:12 schrieb Wilhelm Meier via Interest:
>> Hi all,
>> 
>> does anybody have a minimum working example how to load / save json file
>> contents (auscii contents) to / from a file on android.
>> 
>> I made several attempts, but I get weird filenames and I'm not able to
>> load the stored file.
>> 
>> thx
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>> https://lists.qt-project.org/listinfo/interest <https://lists.qt-project.org/listinfo/interest>
> Hi,
> some snippets HowTo save a JSON. use QVariantMap for a JSON Object or QVariantList for JSON Array
> 
>     QJsonDocument jda = QJsonDocument::fromVariant(myQVariantMap);
>     QByteArray buffer = jda.toJson();
>     saveTestFile("_my_test.json", buffer);
> ...
> 
> void DataServer::saveTestFile(const QString& fileName, const QByteArray& buffer)
> {
>     QString filePath = mDataManager->dataPath(fileName);
>     QFile saveFile(filePath);
>     if (!saveFile.open(QIODevice::WriteOnly)) {
>         qWarning() << "Couldn't open file to write " << filePath;
>         return;
>     }
>     qint64 bytesWritten = saveFile.write(buffer);
>     saveFile.close();
>     qDebug() << "Bytes written: " << bytesWritten << " to: " << filePath;
> }
> ....
> 
>     // Android: AppDataLocation works out of the box, iOS you must create the DIR first !!
>     mDataRoot = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation).value(0);
>     mDataPath = mDataRoot+"/data/";
> ----
> and reading is similar
> 
>     jda = QJsonDocument::fromJson(dataFile.readAll());
>     dataFile.close();
>     if(!jda.isArray()) {
>         myMap = jda.toVariant().toMap();
>     } else {
>  	myList = jda.toVariant().toList();
>     }
> -----
> hope it helps
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org <mailto:Interest at qt-project.org>
> https://lists.qt-project.org/listinfo/interest <https://lists.qt-project.org/listinfo/interest>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210903/2986575e/attachment.html>


More information about the Interest mailing list