How to create a . txt file on Android and make it available to open on computers?

Asked

Viewed 2,083 times

0

My question is regarding how I create files and folders that can be viewed and accessed on later computers. In my code in the project I have a method that creates a text file.

public void criaArquivo() {
        File file = new File (Envoiment.getExternalStoragePublicDirectory("/") + "/testes/TextoFiles.txt");
        FileOutputStream outputStream;

try {
        outputStream = new FileOutputStream (file);
        outputStream.write("string".getBytes());
        outputStream.close();
} catch (IOException e) {
        e.printStackTrace();
}

Well, so far so good it creates a file . txt there on

/Storage/Emulated/0/testes/Textofile.txt

I check this using my Android file managers. It happens that when I open the PC via USB, enabling the MTP appears all the other folders that have in this directory, the

/Storage/Emulated/0/

minus what I just created. Why?

  • No error occurred in writing the file?

  • I can open it by the file manager of my phone. I open it in HTML Reader that Android suggests open with

1 answer

1

I discovered what I was missing to do, I created the file, but only this was not enough, it is necessary to run some lines of code to make the file system of the operating systems recognize the file newly created. Another important factor is that if the file was created while the smartphone is connected in MTP on the computer it is necessary to remove it and put it again so that the file is seen by Windows Explorer, for example. Here is the complementary code for the programme:

...
file.setExecutable(true);
file.setReadable(true);
file.setWritable(true);

MediaScannerConnection.scanFile(this, new String[{file.getAbsolutePath()}, null, null);
...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.