Read and write an txt file from android device
-
hi,
in my app I want to open an file, if it exists, read it and if it doesnt i want to create it. After reading i want to write new source into it.
All my tries failed. I tried it with QFile.open but then the error "device not open" appears...
Is there any other solution to do what I want?best wishes
MathiasEDIT:
btw what path do I have to use?
"file://storage/sdcard/text.txt" or "/sdcard/text.txt" or only "text.txt" or is there any path connected to my app on the device? -
Hi,
You should have a look at the QStandardPaths class, it will help you retrieve the folders that you can access from your application.
Hope it helps
-
Ok, the write-function works perfect but the read-function has got problems.
The file exists on my device and the input.open(QIODevice::ReadWrite) gives no error but if i call readAll() the String is empty. seems like there is a failure.
If I call input.open(QIODevice::ReadOnly) the errorString() returns "No such file or directory" although the file exists because I can write in it...Any other ideas?
I think there is a problem with reading on a android device but dont know why :(
-
Post a minimal example source.
I doubt that there is an issue in Qt because the application would fail to start i it cannot access files atg all. Also the file I/O stuff is part of the Linux Kernel, there should be no difference between a Desktop Linux and Android. -
One working example:
@
#include <QStandardPaths>
#include <QtGlobal>
#include <QFile>
#include <QDir>int main(int, char**)
{
QString folder=QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);
qWarning("folder=%s",qPrintable(folder));QFile* file=new QFile(folder+"/test.txt"); if (!file->exists()) { // create the folder, if necessary QDir* dir=new QDir(folder); if (!dir->exists()) { qWarning("creating new folder"); dir->mkpath("."); } qWarning("creating new file"); file->open(QIODevice::WriteOnly); file->write("Hello World"); file->close(); } if (file->exists()) { qWarning("file exists"); file->open(QIODevice::ReadOnly); QByteArray data=file->readAll(); qWarning("file data=%s",data.constData()); file->close(); }
}
@
-
Maybe the SD card was mounted by the computer and therefore detached from the Android OS ?
-
I'm having the exact same problem you had - trying to read a file on Android I know exists. I have an app that works perfectly on a Windows desktop. On Android I know it is being saved properly since I can read it immediately after saving it and the read works. I'm using the folder provided from QStandardPaths. After I exit the app and re-run it trying to read the same file results in a "File Not Found" message. I tried using the exact code given in another forum post that they said works and the same problem occurs. You said for some reason it started working after many tries. Do you have any idea why it started to work? Is there some kind of persistence flag that needs to be set?
-
I'm having the exact same problem you had - trying to read a file on Android I know exists. I have an app that works perfectly on a Windows desktop. On Android I know it is being saved properly since I can read it immediately after saving it and the read works. I'm using the folder provided from QStandardPaths. After I exit the app and re-run it trying to read the same file results in a "File Not Found" message. I tried using the exact code given in another forum post that they said works and the same problem occurs. You said for some reason it started working after many tries. Do you have any idea why it started to work? Is there some kind of persistence flag that needs to be set?