How to convert and send text in Sketchware as text (.txt) file?

We know that it is not possible to add permissions in Sketchware, and since saving any file requires permissions​ to WRITE EXTERNAL STORAGE, it cannot be done in Sketchware.

But I have found a way to create text (.txt) files in Sketchware from contents of Edittext or Textview, and then share the file using intent. The file can also be saved directly to external storage by using ES File Explorer.

Follow the steps below to create and send text files from your Sketchware App.

1. Insert two Edittext widgets, say edittext1 and edittext2, in VIEW area of your sketchware project. One is for title and other for text.

2. Insert a Button and change it's text to 'Send'.

3. In LOGIC area​ of your project, in onButtonClick event, use add source directly block, and write the following code:

try { java.io.File myFile = new java.io.File(getExternalCacheDir() + "/" + edittext1.getText() + ".txt"); myFile.createNewFile();
java.io.FileOutputStream fOut = new java.io.FileOutputStream(myFile);
java.io.OutputStreamWriter myOutWriter = new java.io.OutputStreamWriter(fOut);
myOutWriter.append(edittext2.getText());
myOutWriter.close();
fOut.close();

Intent email = new Intent(Intent.ACTION_SEND);
email.setType("*/*");
email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new java.io.File(getExternalCacheDir() + "/" + edittext1.getText() + ".txt")));
startActivity(Intent.createChooser(email, "Send: Text File"));

Toast.makeText(getBaseContext(), "File Created with name" + edittext1.getText() + ".txt", Toast.LENGTH_SHORT).show();
}

catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}

Note that in this code we get text from edittext1 and set it as file name, and we set text from edittext2 as file contents.

The code above creates a text file in external cache (which doesn't need permissions to write), and then shares the created text file using intent.

4. Save and run the project. You will be able to share the contents of edittext2 field as text file. And if you have ES file explorer, you can save the text file directly to sdcard.

Here is a video on how I found the code: