How to use QFile on Android



  • I need read .txt file on android using qt program. If build for windows, that file should be in build folder, but what about android? How to include it in apk?
    One person told me use:
    @QFile file( "assets:/qml/foo/main.qml" );@
    but It does not work, maybe I did something not right.
    Can you write step by step instruction?
    (for example: get strings from 1.txt and paste them to QTextEditor on Android)
    Thank you


  • Moderators

    Another way is to add your text file into your program's "resource":http://qt-project.org/doc/qt-5.0/qtcore/resources.html file. And then you can access that file as the other normal resources (e.g images etc..) But it will compiled into the binary.
    E.g Resource file
    @
    <RCC>
    <qresource prefix="/">
    <file>read.txt</file>
    </qresource>
    </RCC>@

    Access as follows:
    @void MainWindow::loadFile()
    {
    QFile file(":/read.txt");
    file.open(QIODevice::ReadOnly);

    QTextStream stream(&file); 
    QString line = stream.readAll();
    file.close(); 
    
    ui->textEdit->setText(line);
    

    }@



  • Thank you.
    I use your code, running on widnows was good, but "installation on Android" crashed,
    Problems:
    @: -1: Error: Could not find the information ELF@
    @: -1: Error: Failed to install package.@
    I inatall on AVD, becouse qt does not see my phone(
    last android project runed fine, I think it is becouse resource have some problems with andoid.


  • Moderators

    Can you give more details ? Which Qt version you are using ? I think this problem is not related to Resource files.



  • @p3c0
    qt5.1.1
    I think that, becouse this problem did not, when I did not use Resource files.

    mainwindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    public slots:
    void loadFile();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H@

    mainwindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QFile>
    #include <QTextStream>

    MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    connect(ui->pushButton, SIGNAL(clicked()), this, SLOT(loadFile()));
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }

    void MainWindow::loadFile()
    {
    QFile file(":/read.txt");
    file.open(QIODevice::ReadOnly);

    QTextStream stream(&file);
    QString line = stream.readAll();
    file.close();
    
    ui->textEdit->setText(line);
    

    }@

    app.qrc
    @<RCC>
    <qresource prefix="/">
    <file>read.txt</file>
    </qresource>
    </RCC>
    @



  • Building log problem:
    @Unable to find '. "
    Make sure that the application is successfully gathered and selected in the Application tab ("start parameters").
    @
    (translate)


  • Moderators

    Please check with your configuration of NDK and SDK's. I would suggest you to create a new application and then deploy to device. The code you provided works on my device. No compilations issues either.



  • Great Thanks=)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.