Write text file in android?

Hi, I’m trying to save some data to a plain text file in android so I can process it later. Does anyone have an example of how to do this?

I don’t want to use the xmlSettings addon as I need the output to be in text file format.
How do I choose the saving directory?

I have tried to add the following code in the ofApp::close() function because I am reading the data from a vector.

// write to file
std::ofstream myfile;

for(std::vector<int>::size_type i = 0; i != dv_timestamps.size(); i++) {
    myfile << dv_timestamps[i] << "\t" << dv_timestamps[i] << "\n";


but it does not work, I can’t find example.txt anywhere in my phone.

Any advice?


You may try

myfile.open(ofToDataPath("") + "example.txt");
1 Like

Thank you for your answer Rancs.

I have also found out that the app does not reach the ofApp::close() function. I’ve checked using the logcat, but the ofLogNotice prints I’ve put in close() do not appear. In the logcat I see statements that the app is on pause (this is when I press the ‘menu’ button or the ‘recent screens’ button), but not that it has been closed.

Do you know what can I do to perform an action, i.e. writing a text file, when the app closes?


That’s not openFrameworks specific thing, but mostly Android’s behaviour: when you press the menu button apps go to pause state. Then Android kills the paused apps when he needs more resources.

I think you may use ofApp::pause() method when pausing the app and ofApp::backPressed() method when closing the app. (But be aware that, even in backPressed(), there is not a real app closing in Android.)

1 Like

Thank you again for your answer Rancs.

I tried to put some ofLogNotice in ofAP::pause() and in ofApp::backPressed, but I can’t see them in the logcat when I exit the app in my phone.

I also put the code in ofApp::touchDown(). There I see the ofLogNotice prints, even the once in the for loop for writing the file. However, I do not find the text file in my phone. How could I force to save the text file in the downloads folder?

I write this into ofApp::setup() and I can see the created file in /sdcard/Android/data/com.mycom.myapp folder:

    ofBuffer dataBuffer;
    dataBuffer.append("Some text for testing..");
    bool fileWritten = ofBufferToFile(ofToDataPath("") + "example.txt", dataBuffer);
1 Like

This one does also create the file in the /sdcard/Android/data/com.mycom.myapp folder:

    ofBuffer dataBuffer;
    dataBuffer.append("Some text for testing..");
    ofFile file;
    file.open(ofToDataPath("") + "example.txt", ofFile::ReadWrite, false);

And this one can append something to the available file:

    ofBuffer dataBuffer;
    dataBuffer.append("Some new text for testing..");
    ofFile file;
    file.open(ofToDataPath("") + "example.txt", ofFile::Append, false);

(I tested all 3 code samples above. They work well.)

I’ve just copied your code into the setup function and I can’t see the file anywhere.
I am using the ES explorer app to search for it, and it does not appear.

I have printed what ofToDataPath("") returns, which is storage/emulated/0/Android/data/cc.openframeworks.exampleTweenPd/files/.
But there is nothing there.

Do you know what might be the reason why?


It had to be there. I can not figure out something at the moment.

If you create a file manually in this directory, can you read it via file.readToBuffer() method?

Rancs you are right, it is there, but it was hidden…!! Thank you for your time :slight_smile: