PDA

View Full Version : QML: save file on Android



mentalmushroom
8th February 2017, 13:58
I have been trying to make a test Android application which asks a user to specify the file name and writes some data there. Starting from the default QtQuick Controls 2 application (Qt 5.7.0), I changed it to provide FileDialog:



import QtQuick 2.7
import QtQuick.Dialogs 1.2

Page1Form
{
MessageDialog {
id: messageDialog
onAccepted: {
Qt.quit()
}
}

FileDialog
{
id: saveFileDialog
title: "Please save a file"
folder: "/sdcard"
selectExisting: false

onAccepted:
{
messageDialog.text = fileWriter.testWrite(saveFileDialog.fileUrl)
messageDialog.visible = true
}
} // FileDialog

button1.onClicked: {
saveFileDialog.visible = true

}
button2.onClicked: {
console.log("Button 2 clicked.");
}
}


In the main.cpp I have this:


#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include <QtCore>

#include "filewriter.h"



int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDp iScaling);
QGuiApplication app(argc, argv);
QQuickStyle::setStyle("Material");


qmlRegisterUncreatableType<FileWriter>("FileWriter", 1, 0, "FileWriter", "FileWriter is uncreatable.");

QQmlApplicationEngine engine;
engine.rootContext()->setContextProperty("fileWriter", new FileWriter);
engine.load(QUrl(QLatin1String("qrc:/main.qml")));

qDebug() << QProcess::systemEnvironment();

return app.exec();
}


A couple of issues emerged:

First, with AA_EnableHighDpiScaling the file dialog is incorrectly displayed on my Sony Xperia Z3:
12328

However, the main window seems to look nice:
12329

Without dpi scaling it looks better, but it is still hardly usable:
12330
It's inconvenient to browse and I don't even see how to type a file name. Is there a more user-friendly way to select files/folders in QML? Any examples? It seems to be a common task.

Despite having added android.permission.WRITE_EXTERNAL_STORAGE to AndroidManifest.xml when I launch the application it has this permission disabled by default. So I have to go to settings on my phone and turn them on manually. Otherwise it doesn't see the SD card content. How can I activate this permission automatically?

Finally, even if I enable WRITE_EXTERNAL_STORAGE permission, FileDialog doesn't seem to honor folder: "/sdcard", showing some default location. But according to the system environment the path exists:

PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin", "ANDROID_BOOTLOGO=1", "ANDROID_ROOT=/system", "ANDROID_ASSETS=/system/app", "ANDROID_DATA=/data", "ANDROID_STORAGE=/storage", "EXTERNAL_STORAGE=/sdcard", "ASEC_MOUNTPOINT=/mnt/asec", "BOOTCLASSPATH=/system/framework/core-libart.jar:/system/framework/conscrypt.jar:/system/framework/okhttp.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/ims-common.jar:/system/framework/apache-xml.jar:/system/framework/org.apache.http.legacy.boot.jar:/system/framework/tcmiface.jar:/system/framework/qcmediaplayer.jar:/system/framework/WfdCommon.jar:/system/framework/oem-services.jar:/system/framework/com.qti.dpmframework.jar:/system/framework/dpmapi.jar:/system/framework/com.qti.location.sdk.jar", "SYSTEMSERVERCLASSPATH=/system/framework/servi

Any help is appreciated