Codenameone 如何使用Codename One和CN1 Filechooser在本地保存和检索文件?

Codenameone 如何使用Codename One和CN1 Filechooser在本地保存和检索文件?,codenameone,Codenameone,我已经研究了代号一网站上的说明。我正在努力 将代号为One的文件选择器选择的文件保存到设备(模拟器、Android、iPhone) 检索本地保存的文件,无论这些文件保存在什么设备上 以下是我使用的代码: final Button getFileChooser = new Button("Get file chooser"); getFileChooser.addActionListener(new ActionListener() { @Override

我已经研究了代号一网站上的说明。我正在努力

  • 将代号为One的文件选择器选择的文件保存到设备(模拟器、Android、iPhone)
  • 检索本地保存的文件,无论这些文件保存在什么设备上
  • 以下是我使用的代码:

     final Button getFileChooser = new Button("Get file chooser");
        getFileChooser.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
    
                if (FileChooser.isAvailable()) {
                    FileChooser.showOpenDialog(".pdf, .jpg, .jpeg, .png", e2-> {
                        String file = (String)e2.getSource();
                        if (file == null) {
                            hi.add("No file was selected");
                            hi.revalidate();
                        } else {
                            String extension = null;
                            if (file.lastIndexOf(".") > 0) {
                                extension = file.substring(file.lastIndexOf(".")+1);
                                ToastBar.showMessage("1: " + file, FontImage.MATERIAL_CHECK);
                                rawFilename.setText("raw: " + file);
                                //java.util.List<String> filepathParsed = StringUtil.tokenize(file, "[/.]");
                                StringBuilder hi = new StringBuilder(file);
                                if (file.startsWith("file://"))
                                    hi.delete(0, 7);
                                int lastIndexPeriod = hi.toString().lastIndexOf(".");
                                Log.p(hi.toString());
                                String ext = hi.toString().substring(lastIndexPeriod);
                                String hmore = hi.toString().substring(0, lastIndexPeriod -1);
    
                                String hi2 = hmore +/* "/file" +*/ ext;/*hi;*/
                                Log.p("hi2 = " + hi2);
                                secondFilename.setText("2nd: " + hi2);
                                //secondFilename.setText((String)evt.getSource());
                                try {
                                    saveFileToDevice(file, ext);
                                } catch (URISyntaxException e) {
                                    e.getMessage();
                                }
                                //sendFileViaEmail(hi2, file);
    
    
                            } else {
                                ToastBar.showMessage("2: " + file + " " + extension, FontImage.MATERIAL_CHECK);
                            }
                        }
                        hi.revalidate();
                    });
                }
            }
        });
    
     protected void saveFileToDevice(String hi, String ext) throws URISyntaxException{
        URI uri = new URI(hi);
        String path = uri.getPath();
        Log.p("uri path: " + uri.getPath() + " ; scheme: " + uri.getScheme());
        /*if (path.contains("..jpg")) {
            int indexLastPeriodBeforeExt = path.lastIndexOf(".");
            path = path.substring(0, indexLastPeriodBeforeExt) + path.substring(indexLastPeriodBeforeExt+ 1);
            Log.p("new path: " + path);
    
        }*/
        if (Display.getInstance().getPlatformName().equals("and"))
            path = "file://" +path;
        String home = FileSystemStorage.getInstance().getAppHomePath();
        char sep = FileSystemStorage.getInstance().getFileSystemSeparator();
        String userDir = home + sep + "myapp";
        /*if (hi.startsWith("file://")) {
            hi = hi.substring(7);
        }*/
        int index = hi.lastIndexOf("/");
        hi = hi.substring(index + 1);
        Log.p("hi after substring 7: " + hi);
        FileSystemStorage.getInstance().mkdir(userDir);
        String fPath = userDir + sep + hi ;
      /*  if (hi.contains("..jpg")) {
            int indexLastPeriodBeforeExt = hi.lastIndexOf(".");
            hi = hi.substring(0, indexLastPeriodBeforeExt) + hi.substring(indexLastPeriodBeforeExt+ 1);
            Log.p("new hi: " + hi);
        }*/
    
            Log.p("does it exist? " + FileSystemStorage.getInstance().exists(userDir));
            String imageFile = userDir + sep + hi;
             String[] roots = FileSystemStorage.getInstance().getRoots();
                if (roots.length > 0) {
                    for (int i = 0; i < roots.length; i++)
                        Log.p("roots" + i + " " + roots[i]);
                }
                try {
                    String[] files = FileSystemStorage.getInstance().listFiles(userDir);
                    for (int i = 0; i < files.length; i++) {
                        Log.p(i + " " + files[i]);
                        //secondName = files[files.length-1];
                        Log.p("secondname: " + secondName);
                    }
                    sendFileViaEmail(imageFile, ".jpg");
                } catch (IOException e) {
                    e.getMessage();
                }
            }
    
    protected void sendFileViaEmail(String hi2, String file) {
        Message m = new Message("Test message");
        //m.getAttachments().put(file, "image/jpeg");
        m.setAttachment(hi2);
        m.setAttachmentMimeType(Message.MIME_IMAGE_JPG);
        //m.setMimeType(Message.MIME_IMAGE_JPG);
        //m.getAttachments().put(imageAttachmentUri, "image/png");
        Display.getInstance().sendMessage(new String[] {"myemail@example.com"}, "test message cn1", m);
    }
    
    如何从
    文件系统存储
    检索此文件的有效、未损坏版本?是否存储错误?
    每当我运行通过电子邮件发送文件的方法时,它会打开Outlook(在我的桌面上),但不会附加任何文件?(我假设该文件在尝试从
    文件系统存储
    检索时无效/已损坏--请帮助。)我将
    .jpg
    硬编码为MIME类型,但仅作为测试;我的目标是能够保存/还原.pdf、.png、.jpg和.jpeg文件

    谢谢


    **编辑:**这似乎表明该文件实际上已保存到我的应用程序的存储器中。如何从我的应用程序中复制/上传或通过电子邮件发送文件内容?

    手机不允许这样做。他们没有像台式机那样的文件系统。所以file chooser真的启动了一个工具来帮助你从不同的应用程序中选择一个文件,然后这个文件被复制到你的本地应用程序沙箱中,在那里你可以访问应用程序的内容

    当然,这对保存不起作用,因为这意味着您可能会损坏另一个应用程序中的文件,因此您被限制写入已安装的其他应用程序的目录,甚至不知道安装了哪些应用程序以及安装在何处


    要将文件传输到不同的应用程序,您可以通过共享按钮或显示的低级API使用共享意图。这允许您向whatsapp发送图像,或在移动电话中使用类似功能

    谢谢。你所说的意图是指像Android中的
    ACTION\u VIEW
    ACTION\u SEND
    这样的意图,这就是你的意思吗?您如何使用Cn1中提到的共享意图?我想将所选文件保存到我的应用程序目录中,以便稍后检索以上载或发送电子邮件。这可能吗?它映射到Android上,但通常只是
    Display.share
    ShareButton
    类。您无法通过电子邮件检索它,因为对于大多数操作系统来说,您的应用程序目录实际上没有一个概念。在iOS和Android上打开gmail,然后单击“附加”,你会看到你得到的并不是一个文件选择器对话框。如果您只关心电子邮件,那么您可以使用send message API,它允许您添加附件,并将启动带有给定附件的操作系统电子邮件客户端…谢谢。只是想知道如何从我的应用程序上传或发送文件内容?有可能吗?附件需要一个已经写入和存储的物理文件,因为您将切换到不同的应用程序来完成此过程,并且移动操作系统为安全性实施了非常强大的应用程序隔离策略谢谢,但没有';难道你没有写到“然后文件被复制到你的本地应用程序沙箱,在那里你可以访问应用程序的内容”?我无法在我的应用程序中访问其内容吗?
     String[] files = FileSystemStorage.getInstance().listFiles(userDir);
                for (int i = 0; i < files.length; i++) {
                    Log.p(i + " " + files[i]);
                    //secondName = files[files.length-1];
                    Log.p("secondname: " + secondName);
                }