codenameone

Attachment file prefix with IMG and date and time stamp


I am using FileSystemStorage to store files that can be shared by the user if they wish. Storing works well however when the user chooses to share (Android device only) the file the attachment is prefixed with IMG_DATE_TIME_ then the filename. This only happens the second time the file is shared. The first attempt is fine. So far this is the same with Gmail, Hotmail and WhatsApp. I have tested this with txt, csv and xlsx files and they all do the same. Am I doing something incorrectly? Any help would be appreciated. Test Case below: -

public void FSStoragePage(Form mainForm){
    Form form = new Form("File System Storage", new BorderLayout());
    form.getToolbar().setBackCommand("", e -> mainForm.showBack());

    Container northcnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    Container centercnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    centercnt.setScrollableY(true);
    Container southcnt = new Container(new BoxLayout(BoxLayout.Y_AXIS));

    TextField tf_fileName = new TextField("", "File name", 3, TextArea.ANY);

    TextField tf_txtBody = new TextField("Text Body", "Enter text", 3, TextArea.ANY);

    Button b_saveTxt = new Button("Save");
    Button b_shareTxt = new Button("Share");

    String s_emailSubject = "";
    Message m = new Message("");
    String s_emailAddr = "";

    FileSystemStorage fs = FileSystemStorage.getInstance();
    String testDir = fs.getAppHomePath() + "testDirectory/";

    b_saveTxt.addActionListener(actionEvent -> {

        boolean bool_exists = fs.exists(testDir);

        if(!bool_exists){

            String testDirNew = fs.getAppHomePath() + "testDirectory/";
            fs.mkdir(testDirNew);

        }

        try (OutputStream os = fs.openOutputStream(testDir + tf_fileName.getText().trim()+".txt")) {

            String fileContent = tf_txtBody.getText().trim();
            os.write(fileContent.getBytes("UTF-8"));

        } catch (IOException err) {
            Log.e(err);
        }

    });

    b_shareTxt.addActionListener(actionEvent -> {

        String filePath = testDir+tf_fileName.getText().trim()+".txt";
        if (fs.exists(filePath)) {
            m.setAttachment(filePath);
            m.setAttachmentMimeType("text/plain");
            Display.getInstance().sendMessage(new String[]{s_emailAddr}, s_emailSubject, m);
        } else {
            Dialog.show("Error", "The file does not exist.", "OK", null);
        }

    });

    centercnt.add(tf_fileName);
    centercnt.add(tf_txtBody);
    centercnt.add(b_saveTxt);
    centercnt.add(b_shareTxt);

    form.add(BorderLayout.NORTH, northcnt).add(BorderLayout.CENTER, centercnt).add(BorderLayout.SOUTH, southcnt);
    form.show();
}

Solution

  • This was discussed here.

    The solution is to define this in your init(Object) method which will disable that behavior:

    Display.getInstance().setProperty("DeleteCachedFileAfterShare", "true");