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:

Comments

  1. Thank you sir for teaching us regarding sketch ware...
    We need more these kind of posts.

    ReplyDelete
  2. Thanks for sharing this information, really appreciate it. Would be great if we could later copy the vscroll as an image and do the same :). Have a great day

    ReplyDelete
  3. HttpURLConnection connection = null;
    DataOutputStream outputStream = null;
    DataInputStream inputStream = null;
    String pathToOurFile = "/data/file_t";
    String urlServer = "http://192.168.1.1/handle_upload.php";
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;
    byte[] buffer;
    int maxBufferSize = 1*1024*1024;

    try
    {
    java.io.FileInputStream fileInputStream = new java.io.FileInputStream(new java.io.File(pathToOurFile) );

    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs.
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Set HTTP method to POST.
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    java.io.outputStream = new DataOutputStream( connection.getOutputStream() );
    java.io.outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    java.io.outputStream.writeBytes(Content-Disposition: form-data() + "/" + edittext1.getText() + ".txt");
    java.io.outputStream.writeBytes(lineEnd);

    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    buffer = new byte[bufferSize];

    // Read file
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

    while (bytesRead > 0)
    {
    outputStream.write(buffer, 0, bufferSize);
    bytesAvailable = fileInputStream.available();
    bufferSize = Math.min(bytesAvailable, maxBufferSize);
    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
    }

    outputStream.writeBytes(lineEnd);
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

    // Responses from the server (code and message)
    serverResponseCode = connection.getResponseCode();
    serverResponseMessage = connection.getResponseMessage();

    fileInputStream.close();
    outputStream.flush();
    outputStream.close();
    }
    catch (Exception ex)
    {
    //Exception handling
    }

    I can't use that code in Skatchware. Why? will you Please make it work?

    ReplyDelete
    Replies
    1. What is the use of this code?
      I could Compile without errors, but to use it, it should be used as a class, which needs to be called later for displaying results.

      Delete
  4. Sir,if want to keep adding the text in the same file.write on same file.how i can??

    ReplyDelete
  5. Sense sketchware removed the "add source block" I've found a workaround easily using a shared preferences file, a webview hidden and writing the java into a html saved directly to the shared preference file and calling it when the app runs using 'script' open and close tags in a dummy html text file saved within the shared preferences file. When your app runs it opens the file wide open for java while hidden from view allowing direct insertion. Hahaha.They pissed me off and can't stop us. Lol

    ReplyDelete

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity