user6635665
user6635665

Reputation: 347

How to write files to an Android device using Qt?

I have problems creating and writing files to an Android device in Qt. I have unsuccessfully tried a few different examples.

This example should write to the internal storage's Download folder:

QString path = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);

qDebug() << path; // '/storage/emulated/0/Download'
qDebug() << "Exists? " << QFile::exists(path + "/401891.png"); // This is an existing image in the folder, returns true

QFile testFile(path.append("/Testing.txt"));
testFile.open(QFile::WriteOnly);

QTextStream out(&testFile);
out << "Hello";

testFile.close();
qDebug() << "Exists? " << QFile::exists(path + "/Testing.txt"); // Returns false

I get this warning:

Warning: QIODevice::write (QFile, "/storage/emulated/0/Download/Testing.txt"): device not open

What I am missing and how to fix it?

Upvotes: 2

Views: 2963

Answers (2)

Mike  Ovchinnikov
Mike Ovchinnikov

Reputation: 1

You are wrong in a last line, it gives:

.../Download/Testing.txt/Testing.txt/

use

qDebug() << "Exists? " << QFile::exists(testFile.fileName()); // Returns true

Upvotes: 0

user6635665
user6635665

Reputation: 347

Seems like the permissions were not being applied although they were stated in Manifest.

Go to Settings -> Applications -> Application Manager

Find your application and ensure the 'Storage' permission toggle is 'On'.

Upvotes: 2

Related Questions