Class SaveFileRequest


public class SaveFileRequest extends FileDialogRequest<SaveFileRequest>

Describes a native Save dialog for com.codename1.ui.Display#showSaveDialog(SaveFileRequest, com.codename1.util.SuccessCallback).

The content is supplied either as an existing file (#sourceFile(String)) or written on demand (#content(SaveContent)), after the user has chosen a location, format and options:

Display.getInstance().showSaveDialog(new SaveFileRequest()
        .suggestedName("Report.pdf")
        .addFilter(new FileTypeFilter("PDF").extensions("pdf").mimeTypes("application/pdf"))
        .addFilter(new FileTypeFilter("Plain text").extensions("txt").mimeTypes("text/plain"))
        .addOption(FileDialogOption.checkbox("charts", "Include charts", true))
        .content((result, out) -> report.export(result.getSelectedFilter(),
                "true".equals(result.getOptionValue("charts")), out)),
    result -> {
        if (result.getError() != null) {
            ToastBar.showErrorMessage("Export failed");
        }
    });

On Android the user picks a document through the Storage Access Framework and the content is streamed into it; on iOS the content is written to a temporary file which the document picker then exports. Either way the application never needs a storage permission.

  • Constructor Details

    • SaveFileRequest

      public SaveFileRequest()
  • Method Details

    • suggestedName

      public SaveFileRequest suggestedName(String name)

      Sets the file name the dialog suggests, including its extension.

      Parameters
      • name: the file name
      Returns

      this request

    • confirmOverwrite

      public SaveFileRequest confirmOverwrite(boolean confirm)

      Asks before replacing an existing file. On by default; the mobile pickers always handle conflicts their own way.

      Parameters
      • confirm: false to replace silently
      Returns

      this request

    • sourceFile

      public SaveFileRequest sourceFile(String path)

      Saves a copy of an existing FileSystemStorage file.

      Parameters
      • path: the file to copy
      Returns

      this request

    • content

      public SaveFileRequest content(SaveContent content)

      Writes the content once the destination is known. Takes precedence over #sourceFile(String).

      Parameters
      • content: the writer
      Returns

      this request

    • getSuggestedName

      public String getSuggestedName()

      The suggested file name, or null.

      Returns

      the name

    • isConfirmOverwrite

      public boolean isConfirmOverwrite()

      Whether replacing a file asks first.

      Returns

      true to confirm

    • getSourceFile

      public String getSourceFile()

      The file being copied, or null.

      Returns

      the path

    • getContent

      public SaveContent getContent()

      The content writer, or null.

      Returns

      the writer

    • writeContent

      public void writeContent(FileDialogResult result, OutputStream out) throws IOException

      Writes this request's content to a stream. Ports call it once the user has chosen a destination; the stream is not closed here.

      Parameters
      • result: what the user chose
      • out: the destination
      Throws
      • IOException: if neither content nor a source file was set, or writing fails
      Throws:
      IOException