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

0

My question is how do I create files and folders that can be viewed and accessed on computers later. 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 .txt file there

  

/storage/emulated/0/testes/TextoFile.txt

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

  

/ storage / emulated / 0 /

except what I just created. Why?

    
asked by anonymous 11.06.2017 / 05:41

1 answer

1

I discovered what I was missing, I created the file, but that's not enough, you need to run a few lines of code to make the operating system filesystem recognize the newly created file. Another important factor is that if the file was created while the smartphone is connected to MTP on the computer it is necessary to remove it and put it back again so that the file is seen by Windows Explorer, for example. Here is the companion code for the program:

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

MediaScannerConnection.scanFile(this, new String[{file.getAbsolutePath()}, null, null);
...
    
12.06.2017 / 00:32